Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
That looks great. But is there a way to do this from 'admin_menu'? What I want to do is have one options page for 3 separate plugins, so in each plugin I need to create this options page if it wasn't created yet by one of the other 2. –  baruch Jan 5 '11 at 7:57

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.

share|improve this answer

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.
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.