How to get URL of admin menu item? (4 posts)

  1. Mickey Kay
    Posted 2 years ago #

    Hi there,

    Is there a simple way to get the URL of admin menu items? I've grabbed the gobal $menu and $submenu arrays which contain quite a bit of info about each menu item, but digging into menu_header.php, it appears that there's quite a bit of conditional logic going on behind the scenes to generate basic urls (e.g. /wp-admin/options-general.php?page=easy-custom-templates, or /wp-admin/admin.php?page=wpcf-import-export). Some of the logic, for example, looks like this:

    if ( ! empty( $menu_hook ) || ( ( 'index.php' != $sub_item[2] ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) && ! file_exists( ABSPATH . "/wp-admin/$sub_file" ) ) ) {
    	// If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
    	if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) )
    		$sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), $item[2] );
    		$sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), 'admin.php' );
    	$sub_item_url = esc_url( $sub_item_url );
    	echo "<li$class><a href='$sub_item_url'$class>$title</a></li>";
    } else {
    	echo "<li$class><a href='{$sub_item[2]}'$class>$title</a></li>";

    Does anyone know of a simple, reliable way to get admin menu item URLs for use in plugins? Thanks!

  2. Hi @McGuive7,

    I hope you are well today and thank you for your question.

    You can use menu_page_url() function to get the url to access a particular menu page based on the slug it was registered with for use in plugins.

    For more information on this function, visit this page http://codex.wordpress.org/Function_Reference/menu_page_url

    Best Regards,

  3. Mickey Kay
    Posted 2 years ago #

    Thanks, I've looked at this function but am unclear as to how to get the slug for a particular item in the admin menu. For example, the global $menu and $submenu arrays don't contain slugs, do they?

    Furthermore, the menu_page_url() searches the global $_parent_pages array, however this array doesn't contain all of my admin menu items.

    Thoughts? Thanks!

  4. Hi @McGuive7,

    The menu_page_url() function only displays URL of particular registered menu page as displayed in the following example and not of all admin menu items.

    add_options_page('Demo Menu', 'Demo Menu', 'manage_options', 'demo_menu', 'demo_menu_options_page');
    menu_page_url( 'demo_menu' );


Topic Closed

This topic has been closed to new replies.

About this Topic