1

I’m developing a new wordpress plugin, I’d like to check if my admin menu slug “xyz” is used bu another plugin in the site.

Is there any method to check the current used slugs?

Please note that I'm looking for slugs in admin menus NOT posts slugs

  • I Found the solution here This is exactly what I'm looking for – Abdallah Sabri Mar 6 '17 at 17:34
1

There is no official way as far as I know. You should be prefixing your menu_slug's with some string that is almost guaranteed to be unique (e.g., 'your_company_name_your_plugin_name_your_menu_slug').

That said, looking into the source for add_submenu_page(), the following should work, but I offer no guarantees (and, see the BIG caveat below):

/**
 * test whether $menu_slug is already used in $parent_slug in admin menus
 *
 * This function should be called **no sooner** than the 'admin_menu' action!
 *
 * Note: this has only been tested in WP 4.7 and relies on undocumented internals,
 * and MIGHT not work in ANY other version of WP!!
 *
 * @param $menu_slug string the proposed menu slug
 * @param $parent_slug string the parent slug
 * @return bool true if $menu_slug already exists in $parent_menu, false otherwise
 */
function
is_menu_slug_available ($menu_slug, $parent_slug)
{
    global $_registered_pages ;

    $menu_slug = plugin_basename ($menu_slug) ;
    $parent_slug = plugin_basename ($parent_slug) ;

    $hookname = get_plugin_page_hookname ($menu_slug, $parent_slug) ;

    return (!(isset ($_registered_pages[$hookname]) && true === $_registered_pages[$hookname])) ;
}

Caveat

It is almost always a BAD IDEA to rely on undocumented internals such as this! Since they are undocumented, the WP team could change how things work under the hood at ANY time and you'd be screwed!

  • if this is really important to you, then you should propose an enhancement to WP thru Trac – Paul 'Sparrow Hawk' Biron Mar 6 '17 at 16:10
  • I understand the (caveat) and appreciate your efforts, but it seems that the global variable $_registered_pages is null – Abdallah Sabri Mar 6 '17 at 16:27
  • $_registered_pages will likely be null if you call the func before the admin_menu action has been fired. But since you shouldn't be calling add_submenu_page() before then, anyway, you should be fine. BTW, are you using WP 4.7? As the comment in the code says, I've only tested it in 4.7, if you're using any other version your "mileage may very". – Paul 'Sparrow Hawk' Biron Mar 6 '17 at 16:37

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.