Paulund
2012-04-30 #wordpress

Controlling The WordPress Toolbar

The Wordpress Toolbar was introduced in Wordpress 3.3 and replaces the Wordpress admin bar which was introduced in 3.1. The purpose of the toolbar was to give logged in users access to dashboard links from the front-end of your website. The toolbar will sit at the top of page in a fixed position and will stay as you scroll down the page.

Many people don't like having the Wordpress toolbar here so they will need an easy way of removing the admin bar. Other people (like me) really like having the toolbar and I like having the additional quick links on the bar. In this tutorial we are going learn how you can add or remove items from the Wordpress toolbar or completely remove the toolbar all together.

Add Items To ToolBar

To add items to the Wordpress toolbar is very easy all you have to use is the Wordpress method add_node(). This is a method of the object $wp_admin_bar so you will use it like this.


<?php $wp_admin_bar->add_node( $args ) ?>

The add_node method takes an argument which is an array of different parameters to pass into the add_node(). Here are the default arguments for the add_node method.


$defaults = array(
        'id' => false, // defaults to a sanitized title value.
	'title' => false,
	'href' => false,
	'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
        'group' => false,
	'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '', tabindex => '' );
);

Add Top level Link

By using the above example we can use the add_node() method to create a link at the top level of admin bar to link back to the home page of the blog. Add the following in to the functions.php file to add a homepage link to the admin bar.


add_action( 'admin_bar_menu', 'add_homepage_admin_bar_link', 500 );

function add_homepage_admin_bar_link( $wp_admin_bar ) {

  $args = array(
    'id' => 'homepage-link',
    'title' => __( 'Homepage' ),
    'href' => home_url()
  );

  $wp_admin_bar->add_node($args);
}

Add Sub-menu Of Links

One of the default arguments on the add_node() method is parent this defines the parent link in the menu. If all links have a parent items then it will create a drop down of all child links. Use the below function to create a top level menu with child items.


add_action( 'admin_bar_menu', 'add_social_links', 500 );

function add_social_links( $wp_admin_bar ) {

  $args = array(
    'id' => 'social-links',
    'title' => __( 'Social Networks' ),
    'href' => false)
  );

  $wp_admin_bar->add_node($args);

// Add child items
$wp_admin_bar->add_node( array(
    'parent' => 'social-links',
    'title' => 'Twitter',
    'href' => 'https://twitter.com/#!/paulund_',
    'meta' => FALSE) );
$wp_admin_bar->add_node( array(
    'parent' => 'social-links',
    'title' => 'Facebook',
    'href' => 'https://www.facebook.com/paulundcouk',
    'meta' => FALSE) );
$wp_admin_bar->add_node( array(
    'parent' => 'social-links',
    'title' => 'Google+',
    'href' => 'https://plus.google.com/u/0/b/110725720433094046987/',
    'meta' => FALSE) );
}

How To Remove Links From Wordpress Admin Bar

To remove the links you just need to add the following function to your functions.php file and add a new action into the wp_before_admin_bar_render function.


function remove_admin_bar_links() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');          // Remove the WordPress logo
    $wp_admin_bar->remove_menu('about');            // Remove the about WordPress link
    $wp_admin_bar->remove_menu('wporg');            // Remove the WordPress.org link
    $wp_admin_bar->remove_menu('documentation');    // Remove the WordPress documentation link
    $wp_admin_bar->remove_menu('support-forums');   // Remove the support forums link
    $wp_admin_bar->remove_menu('feedback');         // Remove the feedback link
    $wp_admin_bar->remove_menu('site-name');        // Remove the site name menu
    $wp_admin_bar->remove_menu('view-site');        // Remove the view site link
    $wp_admin_bar->remove_menu('updates');          // Remove the updates link
    $wp_admin_bar->remove_menu('comments');         // Remove the comments link
    $wp_admin_bar->remove_menu('new-content');      // Remove the content link
    $wp_admin_bar->remove_menu('w3tc');             // If you use w3 total cache remove the performance link
    $wp_admin_bar->remove_menu('my-account');       // Remove the user details tab
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

Disable Admin Bar

If you prefer to keep the Wordpress back-end separate to the front-end then you can remove the admin bar completely for all users. Add the following to the functions.php to disable the admin bar for all users.


show_admin_bar(false);

Disable Admin Bar For Non-Admin Users

If you want to display the admin just for just admin users then you can add the following code to your functions.php file.


if (!current_user_can('manage_options')) {
    show_admin_bar(false);
}

Conclusion

That's just a few code snippets to control the Wordpress toolbar using the above you can add and remove items from the toolbar to make it exactly how you want it. You now also have the option to completely remove the toolbar for all users or for specific users.

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring