WordPress comes with a lot of stuff out of the box. Throw in some plugins, and it gets even noisier. Next minute, your menu sidebar in admin looks like that drawer in the kitchen you shove everything into (known in Australia as a crap draw).
Now, you’ll want to hide some menu items for particular roles or even users (whatever floats your boat). The first hurdle can be knowing what each item is registered as (the slug matters). The core menu items like plugins and tools are easy enough, but custom menu items like “Custom Fields” and “Elite Video Player” — you need to know the slugs these were registered using.
For this, we’ll “pretty print” the menu array (where all menu items live) to discover the slugs. Using this handy little snippet (because I can never remember how to do it using PHP), you’ll get a nice extensive list of menu items.
add_action( 'admin_init', 'get_main_menu_items' ); function get_main_menu_items() { echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>'; }
This will add an enormous ugly printed array inside of your admin panel. What we are looking for is index 2 of the displayed values. This is the slug we need to provide to remove_menu_page
<pre>Array ( [2] => Array ( [0] => Dashboard [1] => read [2] => index.php [3] => [4] => menu-top menu-top-first menu-icon-dashboard menu-top-last [5] => menu-dashboard [6] => dashicons-dashboard ) [4] => Array ( [0] => [1] => read [2] => separator1 [3] => [4] => wp-menu-separator ) [5] => Array ( [0] => Posts [1] => edit_posts [2] => edit.php [3] => [4] => menu-top menu-icon-post open-if-no-js menu-top-first [5] => menu-posts [6] => dashicons-admin-post ) [5.46068] => Array ( [0] => Raw Queue [1] => manage_options [2] => raw-queue [3] => Raw Queue [4] => menu-top menu-icon-generic toplevel_page_raw-queue [5] => toplevel_page_raw-queue [6] => dashicons-admin-generic ) [6] => Array ( [0] => Pinning Queue [1] => manage_options [2] => pinning-queue [3] => Pinning Queue [4] => menu-top menu-icon-generic toplevel_page_pinning-queue [5] => toplevel_page_pinning-queue [6] => dashicons-admin-generic ) [10] => Array ( [0] => Media [1] => upload_files [2] => upload.php [3] => [4] => menu-top menu-icon-media [5] => menu-media [6] => dashicons-admin-media )
Now, in the case of our custom plugin menu item for Elite Video Player, we see this:
[100] => Array ( [0] => Elite Video Player [1] => manage_options [2] => elite_player_admin [3] => Elite Video Player Admin [4] => menu-top toplevel_page_elite_player_admin menu-top-first menu-top-last [5] => toplevel_page_elite_player_admin [6] => dashicons-video-alt3 )
We can see index 2 has a value of “elite_player_admin” this is what we’ll pass to the remove_menu_page
function.
Putting it all together
function theme_hide_menu_items() { remove_menu_page( 'options-general.php' ); // Remove WordPress settings menu remove_menu_page( 'elite_player_admin' ); } add_action('admin_init', 'theme_hide_menu_items'), 999); add_action('admin_menu', 'theme_hide_menu_items', 999);
Now, here is where things get interesting. Most examples you will encounter only use the admin_init
hook, but I also had to use the admin_menu
hook to get items to remove. Even though I had a massive priority of 999, some items were stubborn and adding that second action hook removed them. Try just using one or the other first, my problem was probably something else and I used a machine gun to hammer in some nails.
Removing menu items based on user
This is a super-specific use case, but I had a site where only a particular user could see specific items. I could have used custom permissions/roles, but as you might be aware, that is a lot of messing around. I just wanted to remove menu items if the user wasn’t admin-user
in your case, it might be one or more users.
You could rewrite this to check permissions/roles if you prefer, but this worked for my use case, even if it isn’t the right way to go about it.
function theme_hide_menu_items() { $user = wp_get_current_user(); if ($user->user_login != 'admin-user') { remove_menu_page( 'options-general.php' ); // Remove WordPress settings menu remove_menu_page( 'elite_player_admin' ); } } add_action('admin_init', 'theme_hide_menu_items'), 999); add_action('admin_menu', 'theme_hide_menu_items', 999);
Hopefully, this helped and inspired you.