Removing menus from users other than the administrator
I want to remove some of the menu links from all users except the administrator. How can I do it?
Topic wp-link-pages admin-menu user-roles filters dashboard Wordpress
Category Web
I want to remove some of the menu links from all users except the administrator. How can I do it?
Topic wp-link-pages admin-menu user-roles filters dashboard Wordpress
Category Web
I found the code I was looking for, thank you.
add_action( 'admin_init', 'my_remove_menu_pages' ); function my_remove_menu_pages() { global $user_ID; if ( current_user_can( 'author' ) ) { remove_menu_page( 'tools.php' ); remove_menu_page( 'edit-comments.php' ); remove_menu_page( 'wpcf7' ); remove_menu_page( 'upload.php' ); remove_menu_page( 'index.php' ); } }
If you want to exclude menu items by their labels:
function hide_menu_items( $items ) {
$items_to_exclude = ['Menu Item 1', 'Menu Item 2'];
if ( !current_user_can( 'manage_options' ) ) foreach ($items as $key => $item) if ( in_array( $item->title, $items_to_exclude ) ) unset( $items[$key] );
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'hide_menu_items', 20 );
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.