• Mickey Kay

    (@mcguive7)


    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] );
    	else
    		$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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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 https://codex.www.ads-software.com/Function_Reference/menu_page_url

    Best Regards,

    Thread Starter Mickey Kay

    (@mcguive7)

    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!

    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' );

    Regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get URL of admin menu item?’ is closed to new replies.