Wrestling with the WordPress Toolbar

Of all things WordPress that I do, dealing with the Toolbar has to be the most tedious. I’m never happy with the defaults (Themes, Widgets, Plugins, etc…). I only typically use those during site setup. Here’s a dead simple tidy-up that should serve you and your clients.

 
**
 * Clean up and add items to the Toolbar
 *
 */


function waterstreet_remove_admin_bar_items( $wp_admin_bar ) {
	$wp_admin_bar->remove_node( 'themes' );
	$wp_admin_bar->remove_node( 'menus' );
	$wp_admin_bar->remove_node( 'customize' );
	$wp_admin_bar->remove_node( 'widgets' );
	$wp_admin_bar->remove_node( 'background' );
}
add_action( 'admin_bar_menu', 'waterstreet_remove_admin_bar_items', 999 );


function waterstreet_add_admin_bar_items( $wp_admin_bar ) {
	$args = array(
		'id'    => 'pages',
		'title' => 'Pages',
		'href'  => site_url(). '/wp-admin/edit.php?post_type=page',
		'parent' => 'site-name',
		'meta'  => array( 'class' => 'all-pages' )
		);
	$wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'waterstreet_add_admin_bar_items', 999 );


That first function waterstreet_remove_admin_bar_items works just as you’d expect — it removes items from the main “site-name” dropdown. The second function adds back the items you want.

If you have more than one item, you can just put one after the other in the function:


function waterstreet_add_admin_bar_items( $wp_admin_bar ) {
	$args = array(
		'id'    => 'pages',
		'title' => 'Pages',
		'href'  => site_url(). '/wp-admin/edit.php?post_type=page',
		'parent' => 'site-name',
		'meta'  => array( 'class' => 'all-pages' )
		);
	$wp_admin_bar->add_node( $args );

	$args = array(
		'id'    => 'posts',
		'title' => 'All my Posts',
		'href'  => site_url(). '/wp-admin/edit.php',
		'parent' => 'site-name',
		'meta'  => array( 'class' => 'all-posts' )
		);
	$wp_admin_bar->add_node( $args );


}
add_action( 'admin_bar_menu', 'waterstreet_add_admin_bar_items', 999 );