Customize the WordPress admin menu based on user roles

WordPress can be a powerful content management system but if you have multiple users often some can end up with permissions that you really wish they didn’t have. There are plenty of plugins that will let you customize user permissions but it’s not that hard to do ourselves by removing certain links from the admin menu for those specific user roles.

Note that this method doesn’t actually remove user permissions but just the admin menu links that would get them there.

Basic example
Add the following to functions.php (or create your own plugin) to remove the “Tools” menu from any user with a role lower than Author.

add_action( ‘admin_init’, ‘my_remove_menu_pages’ );

function my_remove_menu_pages() {
// If the user does not have access to publish posts
if(!current_user_can(‘publish_posts’)) {
// Remove the “Tools” menu
remove_menu_page(‘tools.php’);
}
}
Removing the Tools menu

You can choose a variety of user levels rather than publish_posts for example edit_users, delete_pages, etc. View a complete list of WordPress roles and capabilities.

The function remove_menu_page() removes a menu item based off of the menu slug you pass it. To figure out that the menu slug was named tools.php I used used Chrome’s developer tools and right clicked on the Tools menu and selected Inspect element. This showed me that the tools menu was linked as Tools.

To save you the hassle of figuring out each menu slug I listed them below.

Removing sub-menu items
add_action( ‘admin_init’, ‘my_remove_menu_pages’ );

function my_remove_menu_pages() {
// If the user does not have access to add new users
if(!current_user_can(‘add_users’)) {
// Remove the “Link Categories” menu under “Links”
remove_submenu_page( ‘link-manager.php’, ‘edit-tags.php?taxonomy=link_category’ );
}
}
Removing the Tools menu

The function remove_submenu_page() is similar but takes two arguments, the parent menu’s slug and the sub-menu’s slug. Once again you can find the slugs by inspecting each menu link or view the complete list below.

Mixed example
add_action( ‘admin_init’, ‘my_remove_menu_pages’ );

function my_remove_menu_pages() {
if(!current_user_can(‘add_users’)) {
remove_menu_page(‘options-general.php’); // Settings
remove_menu_page(‘tools.php’); // Tools
remove_menu_page(‘upload.php’); // Media

remove_submenu_page( ‘edit.php’, ‘edit-tags.php?taxonomy=category’ ); // Post categories
remove_submenu_page( ‘edit.php’, ‘edit-tags.php?taxonomy=post_tag’ ); // Post tags
}
}

WordPress admin menu slugs
Dashboard remove_menu_page(‘index.php’);
Dashboard remove_submenu_page( ‘index.php’, ‘index.php’ );
Updates remove_submenu_page( ‘index.php’, ‘update-core.php’ );
Posts remove_menu_page(‘edit.php’);
Posts remove_submenu_page( ‘edit.php’, ‘edit.php’ );
Add New remove_submenu_page( ‘edit.php’, ‘post-new.php’ );
Categories remove_submenu_page( ‘edit.php’, ‘edit-tags.php?taxonomy=category’ );
Post Tags remove_submenu_page( ‘edit.php’, ‘edit-tags.php?taxonomy=post_tag’ );
Media remove_menu_page(‘upload.php’);
Library remove_submenu_page( ‘upload.php’, ‘upload.php’ );
Add New remove_submenu_page( ‘upload.php’, ‘media-new.php’ );
Links remove_menu_page(‘link-manager.php’);
Links remove_submenu_page( ‘link-manager.php’, ‘link-manager.php’ );
Add New remove_submenu_page( ‘link-manager.php’, ‘link-add.php’ );
Link Categories remove_submenu_page( ‘link-manager.php’, ‘edit-tags.php?taxonomy=link_category’ );
Pages remove_menu_page(‘edit.php?post_type=page’);
Pages remove_submenu_page( ‘edit.php?post_type=page’, ‘edit.php?post_type=page’ );
Add New remove_submenu_page( ‘edit.php?post_type=page’, ‘post-new.php?post_type=page’ );
Comments remove_menu_page(‘edit-comments.php’);
Appearance remove_menu_page(‘themes.php’);
Themes remove_submenu_page( ‘themes.php’, ‘themes.php’ );
Widgets remove_submenu_page( ‘themes.php’, ‘widgets.php’ );
Menus remove_submenu_page( ‘themes.php’, ‘nav-menus.php’ );
Editor remove_submenu_page( ‘themes.php’, ‘theme-editor.php’ );
Plugins remove_menu_page(‘plugins.php’);
Plugins remove_submenu_page( ‘plugins.php’, ‘plugins.php’ );
Add New remove_submenu_page( ‘plugins.php’, ‘plugin-install.php’ );
Editor remove_submenu_page( ‘plugins.php’, ‘plugin-editor.php’ );
Users remove_menu_page(‘users.php’);
Users remove_submenu_page( ‘users.php’, ‘users.php’ );
Add New remove_submenu_page( ‘users.php’, ‘user-new.php’ );
Your Profile remove_submenu_page( ‘users.php’, ‘profile.php’ );
Tools remove_menu_page(‘tools.php’);
Tools remove_submenu_page( ‘tools.php’, ‘tools.php’ );
Import remove_submenu_page( ‘tools.php’, ‘import.php’ );
Export remove_submenu_page( ‘tools.php’, ‘export.php’ );
Settings remove_menu_page(‘options-general.php’);
General remove_submenu_page( ‘options-general.php’, ‘options-general.php’ );
Writing remove_submenu_page( ‘options-general.php’, ‘options-writing.php’ );
Reading remove_submenu_page( ‘options-general.php’, ‘options-reading.php’ );
Discussion remove_submenu_page( ‘options-general.php’, ‘options-discussion.php’ );
Media remove_submenu_page( ‘options-general.php’, ‘options-media.php’ );
Privacy remove_submenu_page( ‘options-general.php’, ‘options-privacy.php’ );
Permalinks remove_submenu_page( ‘options-general.php’, ‘options-permalink.php’ );

http://sethstevenson.net/customize-the-wordpress-admin-menu-based-on-user-roles/

Deixe um comentário