The menu_page_url() WordPress PHP function retrieves the URL to access a particular menu page based on the slug it was registered with.
Usage
menu_page_url('your-menu-slug', false);
Parameters
- $menu_slug (string): The slug name to refer to this menu by (should be unique for this menu).
- $display (bool): Optional. Whether or not to display the URL. Default: true.
More information
See WordPress Developer Resources: menu_page_url
Examples
Retrieve a menu page URL
This code retrieves the URL for the menu page with the slug ‘my-settings-page’ and stores it in the $menu_page_url
variable.
$menu_page_url = menu_page_url('my-settings-page', false);
Redirect to a menu page
This code retrieves the URL for the menu page with the slug ‘my-settings-page’ and redirects the user to that page.
wp_redirect(menu_page_url('my-settings-page', false)); exit;
Create a link to a menu page in the admin area
This code generates an HTML link to the menu page with the slug ‘my-settings-page’.
echo '<a href="' . menu_page_url('my-settings-page', false) . '">My Settings Page</a>';
Conditional display of a menu page link
This code checks if the menu page with the slug ‘my-settings-page’ exists, and if so, displays a link to it.
if (menu_page_url('my-settings-page', false)) { echo '<a href="' . menu_page_url('my-settings-page', false) . '">My Settings Page</a>'; }
Create a link to a menu page in a plugin
This code creates a link to the menu page with the slug ‘my-settings-page’ within a plugin’s settings page.
function my_plugin_settings_page() { echo '<h2>My Plugin Settings</h2>'; echo '<a href="' . menu_page_url('my-settings-page', false) . '">Go to My Settings Page</a>'; }