Search code examples
phpwordpressquery-variables

Getting current page url on wp-admin/admin dashboard?


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


Solution

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