When you develop websites for clients, maybe sometimes they want to change some elements in admin bar, such as add/remove menus, etc.. how can you do that? I was in that situation and that’s the reason I wrote this post.
Remove nodes from admin bar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_elements' ); function remove_admin_bar_elements() { global $wp_admin_bar; // Remove wp logo $wp_admin_bar->remove_menu( 'wp-logo' ); // Remove comments icon $wp_admin_bar->remove_menu( 'comments' ); // Remove add new media link $wp_admin_bar->remove_node( 'new-media' ); // Remove add new post link $wp_admin_bar->remove_node( 'new-post' ); // Remove go to dashboard link $wp_admin_bar->remove_node( 'dashboard' ); // Remove updates icon $wp_admin_bar->remove_node( 'updates' ); // Remove customize link $wp_admin_bar->remove_node( 'customize' ); // Remove go to widgets link $wp_admin_bar->remove_node( 'widgets' ); // Remove go to menu link $wp_admin_bar->remove_node( 'menus' ); // Remove go to themes link $wp_admin_bar->remove_node( 'themes' ); } |
Tips: To remove any nodes on admin bar, see its id and use it after remove wp-admin-bar-
prefix
1 |
$wp_admin_bar->remove_node( 'new-content' ); |
Add new node to admin bar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
add_action( 'admin_bar_menu', 'my_nodes', 999 ); function my_nodes( $wp_admin_bar ) { $nodes = array( array( 'id' => 'add_product', 'title' => 'Add Product', 'href' => site_url() . '/wp-admin/post-new.php?post_type=product' ), array( 'id' => 'add_book', 'title' => 'Add Book', 'href' => site_url() . '/wp-admin/post-new.php?post_type=book' ), ); foreach ( $nodes as $node ) { $wp_admin_bar->add_node( $node ); } } |
I hope this article is helpful for you!