Hide Admin Menu Items and Admin Bar Items for only one specific User ID

I got a WordPress Website with two Administrators:

Admin with User ID 1 and Admin with User ID 2

I need to hide several items from the wordpress admin menu and from the wordpress admin bar, BUT ONLY for the Administrator with User ID 2

I’m a total noob at this but with a bit of searching and learning I wrote this code and added it in functions.php

//hide in admin menu
add_action( ‘admin_menu’, ‘sc87_hide_menu_items’ );
function sc87_hide_menu_items() {
$whodat = get_current_user_id();
if ($whodat == 2) {
remove_menu_page( ‘index.php’ ); //Dashboard
remove_menu_page( ‘edit.php’ ); //Posts
remove_menu_page( ‘edit.php?post_type=page’ ); //Pages
remove_menu_page( ‘edit-comments.php’ ); //Comments
remove_menu_page( ‘themes.php’ ); //Appearance
remove_menu_page( ‘plugins.php’ ); //Plugins
remove_menu_page( ‘users.php’ ); //Users
remove_menu_page( ‘tools.php’ ); //Tools
remove_menu_page( ‘options-general.php’ ); //Settings
remove_menu_page( ‘wpseo_dashboard’ );
}
}

//hide in admin bar
add_action( ‘wp_before_admin_bar_render’, ‘sc87_hide_bar_items’ );
function sc87_hide_bar_items() {
$whodat = get_current_user_id();
if ($whodat == 2) {
global $wp_admin_bar;
$wp_admin_bar->remove_menu(‘new-page’);
$wp_admin_bar->remove_menu(‘comments’);
$wp_admin_bar->remove_menu(‘new-content’);
$wp_admin_bar->remove_menu(‘updates’);
$wp_admin_bar->remove_menu(‘wp-logo’);
$wp_admin_bar->remove_menu(‘user-info’);
$wp_admin_bar->remove_menu(‘edit-profile’);
$wp_admin_bar->remove_menu(‘search’);
$wp_admin_bar->remove_menu(‘customize’);
$wp_admin_bar->remove_menu(‘archive’);
$wp_admin_bar->remove_menu(‘themes’);
$wp_admin_bar->remove_menu(‘widgets’);
$wp_admin_bar->remove_menu(‘menus’);
}
}

I succeeded in removing all the stuff that I needed to get removed, and it looks like is working perfectly, it only affects the Admin with User ID 2, and that’s what I wanted, but since I’m a noob at coding I wanted to ask you this:

Is there a better and/or more efficient way to code this?
Maybe by using the whodat and if just once? (I’m just guessing since I have no idea if this is possible)

Thank you in advance for your help.

Read more here:: Hide Admin Menu Items and Admin Bar Items for only one specific User ID

Leave a Reply

Your email address will not be published. Required fields are marked *