Remove menu items in wp-admin depending on user role
So lets say you open up your blog to guest posters, and you create a user for them, and possibly even a custom role.
You will usually find one or more menu items that you don’t want to show to your guest posters. How do you hide them?
Just take a look at the url of those menu items, and then use that to build up your function like this:
/* Remove Tools admin menu item for everyone other than Administrator */ add_action( 'admin_init', 'remove_menu_pages_for_all_except_admin' ); function remove_menu_pages_for_all_except_admin() { global $user_ID; if ( !current_user_can('administrator') ) { remove_menu_page('tools.php', 'tools.php'); } }
Here are some of menu page names for the most common menu items that come with WordPress:
remove_menu_page('edit.php'); // Posts remove_menu_page('upload.php'); // Media remove_menu_page('link-manager.php'); // Links remove_menu_page('edit-comments.php'); // Comments remove_menu_page('edit.php?post_type=page'); // Pages remove_menu_page('plugins.php'); // Plugins 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
Here’s one other bonus tip for you. If you want to remove the ability of a role to see the list of posts by other users, use this code:
add_action( 'load-edit.php', 'posts_for_current_contributor' ); function posts_for_current_contributor() { global $user_ID; if ( current_user_can( 'contributor' ) ) { if ( ! isset( $_GET['author'] ) ) { wp_redirect( add_query_arg( 'author', $user_ID ) ); exit; } } }