Blog

Customize the WordPress admin menu based on user roles

wordpress-logo-stacked-bg

 

If you have multiple users for WordPress administration,  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.

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.

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

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');
  }
}

 

You can choose a variety of user levels rather than publish_posts for example edit_usersdelete_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<a href="tools.php" tabindex="1">Tools</a>.

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' );
}
}

 

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

Dashboardremove_menu_page(‘index.php’);
Dashboardremove_submenu_page( ‘index.php’, ‘index.php’ );
Updatesremove_submenu_page( ‘index.php’, ‘update-core.php’ );

Postsremove_menu_page(‘edit.php’);
Postsremove_submenu_page( ‘edit.php’, ‘edit.php’ );
Add Newremove_submenu_page( ‘edit.php’, ‘post-new.php’ );
Categoriesremove_submenu_page( ‘edit.php’, ‘edit-tags.php?taxonomy=category’ );
Post Tagsremove_submenu_page( ‘edit.php’, ‘edit-tags.php?taxonomy=post_tag’ );
Mediaremove_menu_page(‘upload.php’);
Libraryremove_submenu_page( ‘upload.php’, ‘upload.php’ );
Add Newremove_submenu_page( ‘upload.php’, ‘media-new.php’ );
Linksremove_menu_page(‘link-manager.php’);
Linksremove_submenu_page( ‘link-manager.php’, ‘link-manager.php’ );
Add Newremove_submenu_page( ‘link-manager.php’, ‘link-add.php’ );
Link Categoriesremove_submenu_page( ‘link-manager.php’, ‘edit-tags.php?taxonomy=link_category’ );
Pagesremove_menu_page(‘edit.php?post_type=page’);
Pagesremove_submenu_page( ‘edit.php?post_type=page’, ‘edit.php?post_type=page’ );
Add Newremove_submenu_page( ‘edit.php?post_type=page’, ‘post-new.php?post_type=page’ );
Commentsremove_menu_page(‘edit-comments.php’);

Appearanceremove_menu_page(‘themes.php’);
Themesremove_submenu_page( ‘themes.php’, ‘themes.php’ );
Widgetsremove_submenu_page( ‘themes.php’, ‘widgets.php’ );
Menusremove_submenu_page( ‘themes.php’, ‘nav-menus.php’ );
Editorremove_submenu_page( ‘themes.php’, ‘theme-editor.php’ );
Pluginsremove_menu_page(‘plugins.php’);
Pluginsremove_submenu_page( ‘plugins.php’, ‘plugins.php’ );
Add Newremove_submenu_page( ‘plugins.php’, ‘plugin-install.php’ );
Editorremove_submenu_page( ‘plugins.php’, ‘plugin-editor.php’ );
Usersremove_menu_page(‘users.php’);
Usersremove_submenu_page( ‘users.php’, ‘users.php’ );
Add Newremove_submenu_page( ‘users.php’, ‘user-new.php’ );
Your Profileremove_submenu_page( ‘users.php’, ‘profile.php’ );
Toolsremove_menu_page(‘tools.php’);
Toolsremove_submenu_page( ‘tools.php’, ‘tools.php’ );
Importremove_submenu_page( ‘tools.php’, ‘import.php’ );
Exportremove_submenu_page( ‘tools.php’, ‘export.php’ );
Settingsremove_menu_page(‘options-general.php’);
Generalremove_submenu_page( ‘options-general.php’, ‘options-general.php’ );
Writingremove_submenu_page( ‘options-general.php’, ‘options-writing.php’ );
Readingremove_submenu_page( ‘options-general.php’, ‘options-reading.php’ );
Discussionremove_submenu_page( ‘options-general.php’, ‘options-discussion.php’ );
Mediaremove_submenu_page( ‘options-general.php’, ‘options-media.php’ );
Privacyremove_submenu_page( ‘options-general.php’, ‘options-privacy.php’ );
Permalinksremove_submenu_page( ‘options-general.php’, ‘options-permalink.php’ );

    Add Comment