I’ve got the following code, which cleans up a lot of stuff that is not going to be used within the admin area:
add_action( 'admin_menu', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
remove_menu_page( 'edit.php' ); //Posts
remove_menu_page( 'upload.php' ); //Media
remove_menu_page( 'edit-comments.php' ); //Comments
remove_menu_page( 'themes.php' ); //Appearance
remove_menu_page( 'users.php' ); //Users
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'options-general.php' ); //Settings
};
However, there are two menu items that have been inserted from plugins.
When I hover over each menu item, it says the links are:
/wp-admin/edit.php?post_type=acf
/wp-admin/admin.php?page=wpcf7
Is there a way to hide these menu pages, too?
You need to use the right hooks (which are not always the same as the URLs/slugs), and it doesn’t hurt to use a hook that runs later (e.g.,
admin_init
):You can use the following to debug:
This gives (for my setup) the following for the Contact Form 7 plugin menu page:
The array element with key
2
is what you are looking for:wpcf7
.YOU CAN DEBUG ALL THIS WITH THE FOLLOWING TO GET ALL THE INFO YOU NEED:
The keys will give you the array values that allow you do all of this with wordpress globals (although not recommended)
And to remove for certain users just do the same thing except with capabilities added:
AND TO TIE IT ALL TOGETHER WHY NOT SHORTEN OUR CODE? YOU CAN USE ARRAYS TO AVOID WRITING remove_submenu_page 50 times. This also works with the toolbar nodes:
You probably need to set the add_action priority to a higher number and target those two new links specifically like so:
Advanced Custom Fields also provides a help document on how to do this for their menu here:
http://www.advancedcustomfields.com/resources/how-to/how-to-hide-acf-menu-from-clients/
Contact Form 7 also has it’s own way of restricting the visibility of the menu:
http://contactform7.com/restricting-access-to-the-administration-panel/
Update
I created a code snippet with a more robust function that deals with both main menus and sub-menu items.
Original answer
Instead of specifying the menus you want to remove, specify the menues you want to keep 🙂
This way you don’t have to search for plugin names and modify the code when you add new plugins..
You need to find the right $menu_slug. The following code worked for me:
Of course you can specify only the items you want to remove. Have a look in the code below:
This is just the negative of numediaweb’s method. Thanks @numediaweb. It works fine.
P.S.:
‘menu_item_1/n’ -> point mouse over the menu item and fetch the exact page of that menu item shown in the link.
use this code snippet