1 min read

Remove Wordpress menus for specific users

It’s not that difficult to remove Wordpress menus and not much more effort to allow only certain menus for certain users, regardless of role.

The following unsets menu options when the user ID matches 1. This allows you to still grant ‘Administrator’ privilages to clients but prevent them from accessing certain menu items that might contain options that break the site if changed.

Copy the following into your functions.php file and delete where necessary. Uncomment lines 9-12 to print the options at the top of the Wordpress dashboard.

<?php
add_action( 'admin_menu', 'remove_menus' );
function remove_menus() {
global $menu;
global $submenu;
// echo '<pre>';
// print_r($menu);
// print_r($submenu);
// echo '</pre>';
// If you're not the site creator
// Hide some menus
if ( wp_get_current_user()->ID != 1 ) {
unset($menu[25]); // Removes 'Comments'.
unset($menu[65]); // Removes 'Plugins'.
unset($submenu['index.php'][10]); // Removes 'Updates'.
unset($submenu['themes.php'][5]); // Removes 'Themes'.
unset($submenu['themes.php'][6]); // Removes 'Customize'.
unset($submenu['themes.php'][11]); // Removes 'Editor'.
}
}
?>
view raw remove.php hosted with ❤ by GitHub

It might be the case that you only need to rename certain menu options to bring them inline with the site’s content and to make the interface more intuitive.

Copy and paste the following into your functions.php and — using the same principles above to determine the desired menu option — change any menu option to whatever you like. The example changes ‘posts’ to ‘news’.

<?php
add_action( 'admin_menu', 'rename_posts_menu' );
// echo '<pre>';
// print_r($menu);
// print_r($submenu);
// echo '</pre>';
function rename_posts_menu() {
global $menu;
global $submenu;
$menu[5][0] = 'News';
$submenu['edit.php'][5][0] = 'News Items';
$submenu['edit.php'][10][0] = 'Add News Item';
}
?>
view raw rename.php hosted with ❤ by GitHub

You could even rename menus for a specific user.

Posted