6

Im trying to get the current page url while on the wp-admin/admin dashboard, is it possible?

Im trying to use these codes but i can't seem to get it to work.

global $wp;
$current_page = add_query_arg( $wp->query_vars, home_url( $wp->request ) );

The output i wanted is like this: https://example.com/1/wp-admin/admin.php?page=test But instead, the output turns like this: /1/wp-admin/admin.php?page=test&=https://example.com/1

Any help is greatly appreciated

2
  • If you just want the current URL, you should be able to use $_SERVER['REQUEST_URI'] which is what add_query_arg uses behind the scenes. I think you can also technically say $url = add_query_arg([]); to get it, too
    – Chris Haas
    Nov 13, 2020 at 22:24
  • @ChrisHaas i already tried both of those, and this is the output i get: /1/wp-admin/admin.php?page=test The main domain from the url isnt on the output.
    – Amenadiel
    Nov 13, 2020 at 22:38

4 Answers 4

6

I've found a very handfull solution using http_build_query() function :

$current_page = admin_url(sprintf('admin.php?%s', http_build_query($_GET)));

http_build_query() will automatically transforms all $_GET params into a query string which can be append to the base admin URL using sprintf().

However this does not support pages with a different name (eg: edit.php instead of admin.php). You may use something like this instead, as a helper function, to support every admin pages :

/**
 * Get the base URL of the current admin page, with query params.
 *
 * @return string
 */
function get_current_admin_url(): string
{
    return admin_url(sprintf(basename($_SERVER['REQUEST_URI'])));
}

# on your admin page..
$current_page = get_current_admin_url();

Beware of the use of this function, URLs can be manipulated by the end user /!\

1
5

To retrieve the URL of the admin area of your wordpress site, you can use admin_url(): https://developer.wordpress.org/reference/functions/admin_url/

It accepts a path. So you could do admin_url('admin.php?page=test'); if you like to hard code. But you want to have it dynamically I guess.

I assume, your page is part of the admin menu. Every menu page automatically has a page parameter in the URL like page=test.

You want to get the URL of the current page you are looking at, so you can get the page parameter from the URL using $_GET.

Putting all this information together, your code can look like:

$current_page = admin_url( "admin.php?page=".$_GET["page"] );

1
  • 1
    I see, i really have to use t he get request. I learn everyday, thanks a lot!!
    – Amenadiel
    Nov 15, 2020 at 3:21
2

You can use the global $pagenow or $typenow to get the current admin page.

For example, if you want to target only edit.php?post_type=page, you can use the following :

global $pagenow, $typenow;
  if( ! is_admin() || 'edit.php' !== $pagenow || 'page' !== $typenow ) return;
2

Just use add_query_arg() function that automatically detects current page URL (relative) - https://developer.wordpress.org/reference/functions/add_query_arg/

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.