Admin Bar Menu Editing
-
Hiya,
So apologies if this exists already but my searched didn’t yield any suitable results.I used this code in my functions.php of my theme:
function annointed_admin_bar_remove() { global $wp_admin_bar, $current_user; if ($current_user->ID != 2) { $wp_admin_bar->remove_menu('wp-logo'); // Remove the WP Logo Menu $wp_admin_bar->remove_menu('site-name'); // Remove the site name menu $wp_admin_bar->remove_menu('view-site'); // Remove the view site link } } add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove');
The issue is that I want it to show for my admin accounts still (User ID 1 & 2 but trying 2 for now) but it still is disabled for all. What am I missing and how do I get it to work with both user IDs? Thanks in advance for all assistance / replies!
-
Hi,
Are you sure your $current_user already has data at that moment ?Just before your if statement, try to run
get_currentuserinfo();
And just to make a quick test, you could always echo the ID just after running the above function to verify
echo 'User ID: ' . $current_user->ID . "\n";
Pascal.Foremost I thank you for your assistance , you totally rock because it actually works now!
Now my follow up question is a bit dumb but how would I include multiple admin user IDs?
Hi,
No dumb questions here…As you might have seen in your database, the ID field is a bigint, so to continue on the IF that you used, you could go for:
$myadmins = array(1, 2); // Add all IDs of the admins in this array if ( !in_array($current_user->ID, $myadmins) ) {
Pascal.
That worked beautifully! You totally and completely rock….HARD!
A huge thank you! I wish there was a button I could click to thank you with or something but all I can think to do is share my code so it can help others / hopefully prevent you from having to answer the same question again.Here is what I put to make it work:
<?php function annointed_admin_bar_remove() { global $wp_admin_bar, $current_user; get_currentuserinfo(); $myadmins = array(1, 3); // Add all IDs of the admins in this array if ( !in_array($current_user->ID, $myadmins) ) { $wp_admin_bar->remove_menu('wp-logo'); // Remove the WP Logo Menu $wp_admin_bar->remove_menu('site-name'); // Remove the site name menu $wp_admin_bar->remove_menu('view-site'); // Remove the view site link } } add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove');
- You must be logged in to reply to this topic.