11

I'm working on a plugin that I have to release in parts.

The problem is, if there is a menu in the page then add the new plugin to the 2nd or next submenu else add a new menu and then add the plugin to 1st submenu.

My questions:

  1. How to check that there exists a menu?
  2. If I'm coding a reusable function , that will go with every plugin then that causes conflict option.
  3. I don't know the order of the plugin release.
add_menu_page(
    'Page Title',
    'Top Menu Title',
    'manage_options',
    'my_unique_slug',
    'my_magic_function'
);
add_submenu_page(
    'my_unique_slug',
    'page title',
    'submenu title',
    'manage_options',
    'my_submenu_slug',
    'my_magic_function_of_submenu'
);
17

You can use the fourth parameter of add_menu_page(), the my_unique_slug, to check if the page exists:

if ( empty ( $GLOBALS['admin_page_hooks']['my_unique_slug'] ) )
    add_menu_page(
        'Page Title',
        'Top Menu Title',
        'manage_options',
        'my_unique_slug',
        'my_magic_function'
    );

$GLOBALS['admin_page_hooks'] is the list of registered pages.

  • 1
    FYI, you can access the global on its own: global $admin_page_hooks; – Nathan Hangen Apr 19 '17 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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