Remove Specific Items from the Top Admin Menu Bar

Remove specific items from the top admin menu bar image

You know that menu bar that shows up at the top of your WordPress site when you’re logged in?  You know, the one that says your site’s name and has the “+New” dropdown menu? Yea, that one!…….GET RID OF IT!!

What it does…

Removes specific items from the top admin menu bar that shows when a user is logged in.

Why it does it…

So you can keep certain items hidden from certain users… or just pretty up the joint if you want.

How it does it…

Hooks to `wp_before_admin_bar_render` and specifically removes the items you tell it to.

See the code…

<?php

// Hide select items from the top menu bar that shows when a user is logged in.
// https://snipsnip.pro/s/249
namespace TKIqaz2Q;

function remove_items_from_top_admin_menu() {
    $user = \wp_get_current_user();
    // if current user is an admin, don't make any changes
    if (\current_user_can('manage_options')) { return ; }
    // or if you need to modify for only certain roles, 
    // you could do something like: if (in_array('editor',$user->roles)) {}
    global $wp_admin_bar;
    // add lines here for what you wish to remove; find the correct 
    // keyword by looking at the html ID of the item you want to remove
    // remove the comments item
    $wp_admin_bar->remove_menu('comments');
    // remove the site name
    $wp_admin_bar->remove_menu('site-name');
    // remove the WordPress logo
    $wp_admin_bar->remove_menu('wp-logo');
    // remove the User item in the +New menu
    $wp_admin_bar->remove_menu('new-user');
	// or remove the entire +New menu
    $wp_admin_bar->remove_menu('new-content');
}
\add_action( 'wp_before_admin_bar_render', '\TKIqaz2Q\remove_items_from_top_admin_menu' );