How to check if an admin (sub)menu already exists?

The title says it all. I’m using WP 3.0.4

Related posts

Leave a Reply

4 comments

  1. Something like this should work. $handle should be the menu’s slug; set $sub to true to search submenus (defaults to top level menus):

    function find_my_menu_item( $handle, $sub = false; ){
      if( !is_admin() || (defined('DOING_AJAX') && DOING_AJAX) )
        return false;
      global $menu, $submenu;
      $check_menu = $sub ? $submenu : $menu;
      if( empty( $check_menu ) )
        return false;
      foreach( $check_menu as $k => $item ){
        if( $sub ){
          foreach( $item as $sm ){
            if($handle == $sm[2])
              return true;
          }
        } else {
          if( $handle == $item[2] )
            return true;
        }
      }
      return false;
    }
    

    One last quick note: this should not be used before the menu is set. 'admin_init' is a safe bet for the earliest time to use this. I also added some code to leave gracefully if the menus aren’t set.

  2. Here is a simpler way of doing it:

    global $submenu;
    
    $main_menu = 'my_menu_slug';
    
    if (
        isset( $submenu[ $main_menu ] )
        && in_array( 'my_submenu_slug', wp_list_pluck( $submenu[ $main_menu ], 2 ) )
    ) {
        // Submenu exists.
    } else {
        // Submenu doesn't exist.
    }
    
  3. Sweet! and here I thought I was the only crazy person who wanted to do something like this!

    Well, based on what I think you’re wanting (something similar to what I posted here), then here’s the solution I came up with (taking advantage of the function given above)

    Originally I thought I was going to have to check to see which of my plugins were active each time a new plugin was activated.

    I had figured I’d use is_plugin_active() to see which menu items to display beneath the parent “main” menu item… However, this turned out to be harder than I thought because that function isn’t activated yet when the plugin accesses the file that all this would be in.

    Thankfully, it wound up being so simple it was staring me in the face the whole time… Each plugin that’s activated can simply add that plugin’s submenu to the parent menu without having to worry about checking for the existence of other active plugins…

    I added this to each of “My” plugins (aka, the plugins my company released)

    if(!function_exists('find_my_menu_item')) {
      function find_my_menu_item($handle, $sub = false) {
        if(!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
          return false;
        }
        global $menu, $submenu;
        $check_menu = $sub ? $submenu : $menu;
        if(empty($check_menu)) {
          return false;
        }
        foreach($check_menu as $k => $item) {
          if($sub) {
            foreach($item as $sm) {
              if($handle == $sm[2]) {
                return true;
              }
            }
          } 
          else {
            if($handle == $item[2]) {
              return true;
            }
          }
        }
        return false;
      }
    }
    

    Then I added this little snippet to each of the plugins:

    if(!function_exists('wpg_add_parent_page')) {
      function wpg_add_parent_page() {
        if(!find_my_menu_item('main_item_slug')) {
          add_menu_page('Main Item Page Title','Main Item Menu Title', 'activate_plugins', 'main_item_slug', 'main_item_function', THISPLUGIN_PATH.'images/menu-icon.png');
        }
        if(!function_exists('remove_submenu_page')) {
          unset($GLOBALS['submenu']['main_item_slug'][0]);
        }
        else {
          remove_submenu_page('main_item_slug','main_item_slug');
        }
      }
      add_action('admin_menu', 'wpg_add_parent_page');
    }
    if(!function_exists('wpg_add_thisplugin_page')) {
      function wpg_add_thisplugin_settings_page() {
        add_submenu_page('main_item_slug', 'ThisPlugin Settings', 'ThisPlugin Menu Item', 'activate_plugins', 'this_plugin_slug', 'wpg_thisplugin_function');
      }
      add_action('admin_menu', 'wpg_add_thisplugin_page');
    }
    

    Just modify that for each of the plugins you wish to add under one menu item, and insert the plugin appropriate modified version in each of the relevant plugins.

  4. Here’s a nice terse way of doing it without all those conditionals and foreach loops.

    function submenuExists($submenuTitle)
    {
    
        global $submenu;
    
        return count(array_filter($submenu['plugins.php'], 
            function($submenuPage) use ($submenuTitle) {
                return $submenuPage[0] == submenuTitle;
            }
        )) > 0;
    }