Getting current page url on wp-admin/admin dashboard?
Asked Answered
D

4

8

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

Donatello answered 13/11, 2020 at 22:20 Comment(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, tooSaving
@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.Donatello
F
6

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"] );

Fantom answered 14/11, 2020 at 0:49 Comment(1)
I see, i really have to use t he get request. I learn everyday, thanks a lot!!Donatello
A
7

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 /!\

Ament answered 23/9, 2021 at 6:25 Comment(1)
there is one issue I found which is if you echo the $current_page when you are at the wp dashboard (HTTP://site.local/wp-admin), then it says site.local/wp-admin/wp-admin. That means the condition is not working while I am at the dashboard (HTTP://site.local/wp-admin)Novara
F
6

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"] );

Fantom answered 14/11, 2020 at 0:49 Comment(1)
I see, i really have to use t he get request. I learn everyday, thanks a lot!!Donatello
B
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;
Bhayani answered 15/12, 2021 at 13:36 Comment(0)
D
2

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

Durative answered 27/7, 2022 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.