WordPress Get Current Admin Page URL
Do you want to know how: WordPress Get Current Admin Page URL
During WordPress development, it is possible to dynamically retrieve the URL of the current admin page. At times, we are required to generate dynamic links and add scripts to enhance the functionality of themes or plugins.
In this article, we’ll explain different WordPress methods to get the current admin page URL
Why We Need: WordPress To Get Current Admin Page URL
In plugin and theme development, it is a common requirement to retrieve the current admin page URL for several reasons.
Developers can create more dynamic and customized solutions by finding the current admin page URL.
Dynamic links, which are frequently required by developers in the WordPress admin area, can be created by using the current admin page URL.
Developers can load scripts and styles only on specific pages by identifying the current admin page URL. This strategy enhances the functionality of the WordPress admin interface.
In plugins and themes, developers can implement conditional logic by retrieving the current admin page URL. By using this, functionalities can be enabled or disabled according to the user’s location within the admin interface.
Loading plugin functionalities with Third-Party APIs in admin areas can also be made easier with this.
Also we need admin URL to create navigational link in menu pages of WordPress admin pages.
Methods: WordPress Get Current Admin Page URL
In this tutorial, we will explain different methods to get current admin page URL in WordPress.
$_SERVER To Get Admin URL
$_SERVER is a PHP global variable that is also called Superglobals. The information contained in it relates to the server and execution environments. Any class, function, or file can access these variables because they are pre-defined.
We can use $_SERVER with argument [‘REQUEST_URI’] to get the admin URL.
$_SERVER[‘REQUEST_URI’]
The $_SERVER[‘REQUEST_URI’] variable is responsible for storing information about the current request, which includes the URL. Extracting the necessary details from this variable enables us to construct the URL for the admin page.
echo $_SERVER[‘REQUEST_URI’];
//Output: /wp-admin/admin.php?page=add-donation
Use of admin_url() And $_SERVER To Get Full Admin URL
Example:
Assume that we are on the ‘Plugins’ page in the WordPress admin area. To obtain the current admin page URL, we can use the $_SERVER[‘REQUEST_URI’] method.
Method 1.
$current_admin_page_url = admin_url($_SERVER['REQUEST_URI']);
echo $current_admin_page_url;
//output: https://example-webiste/wp-admin/wp-admin/admin.php?page=add-donation
Method 2.
$current_admin_page_url = rtrim(admin_url(),'/'). $_SERVER['REQUEST_URI'];
echo $current_admin_page_url;
//output: https://example-webiste/wp-admin/wp-admin/admin.php?page=add-donation
Here in the previous code, we get the URL of the add-member page which is a menu page in the admin area.
Use of get_admin_url() And $_SERVER To Get Full Admin URL
The get_admin_url() function in WordPress is a useful function to retrieve the current admin page URL. Specific URLs can be displayed in the admin panel by passing parameters such as a specific path or query string to these functions.
$current_admin_page_url = get_admin_url(null, $_SERVER['REQUEST_URI']);
echo $current_admin_page_url;
//Output: https://example-webiste/wp-admin/wp-admin/admin.php?page=add-donation
Use of $pagenow for Current Admin Page
WordPress has another global variable $pagenow. It contains the filename of the admin page that is currently being accessed.
Getting admin File URL Using $pagenow
The current admin page URL can be constructed by combining the $pagenow variable with admin_url().
global $pagenow;
$current_admin_page_url = admin_url($pagenow);
echo $current_admin_page_url;
//Output: https:/example-webiste/wp-admin/admin.php
In this code admin_url() give https://example-webiste/wp-admin/
And $pagenow give admin.php
Checking Admin Using $pagenow
In the following example, we can use $pagenow for the condition if the current page admin page
Example of using $pagenow
global $pagenow;
if ( $pagenow == 'admin.php' ) {
<script type="text/javascript">
// JavaScript code
</script>
}
Using Script on a Specific Admin Page
By using the $pagenow global variable we can create a condition so that a script can be used on a specific admin page.
function enqueue_admin_script() {
global $pagenow;
// Check if we are on the "Add Member Admin Page"
if ($pagenow === 'admin.php' && isset($_GET['page']) && $_GET['page'] === 'add-member') {
wp_enqueue_script('admin-script', plugin_dir_path() . '/js/admin-script.js', array(), '2.0', true);
}
}
add_action('admin_enqueue_scripts', 'enqueue_admin_script');
Here, we use the $pagenow global variable to check if it is admin.php, and we use $_GET[‘page’] to check if it is a specific page, in our case, the add-member menu page in admin.php. Here If statement is used to check the condition.
Using the get_current_screen() Function.
Information about the current admin screen can be obtained through the get_current_screen() function. To retrieve the base URL of the current admin page, you can use this function.
$current_screen = get_current_screen();
$current_admin_page_url = admin_url($current_screen->base);
//Output: https://example-website/wp-admin/member_page_add-member
This function in the plugin allows you to locate the admin page menu and other items.
Conclusion
Obtaining the current admin page URL in WordPress is necessary for various development tasks. Three methods, admin_url(), get_admin_url(), and $pagenow, were explained in this article. Our explanation includes the use of $_SERVER[‘REQUEST_URI’] and its integration with two methods to obtain the full URL.
We hope this tutorial will help you create dynamic links, and custom functionality, and implement conditional actions on the current admin page.