Web Niraj
  • Facebook
  • Flickr
  • Github
  • Instagram
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • WordPress: Customising the Toolbar / Admin Bar

WordPress: Customising the Toolbar / Admin Bar

0

I recently developed a WordPress plugin for Cage Cricket to integrate it’s mobile app with the website. One of the features I used in the plugin was customising the Admin Bar / Toolbar to add in custom images and links. The WordPress hooks make is relatively easy to customise the toolbar to achieve what you want.

Plugin Template

To start off, you first need to create your plugin file and add in any hooks and functions. The below template tells WordPress to load any custom menu functions using the add_action hook.

<?php
/**
* @package MyToolbar
*/
/*
Plugin Name: MyToolbar
Plugin URI: http://www.webniraj.com
Description: This plugin creates a custom toolbar
Version: 1.0
Author: Niraj Shah
Author URI: http://www.webniraj.com/wordpress/
*/
// function to run before menu is rendered
add_action( 'wp_before_admin_bar_render', 'preload_my_toolbar' );
// function to add items to toolbar
add_action( 'admin_bar_menu', 'my_toolbar' );
// forces toolbar to show to all users
add_filter( 'show_admin_bar', '__return_true' );
// placeholder function
function preload_my_toolbar() {
global $wp_admin_bar;
}
// placeholder function
function my_toolbar() {
global $wp_admin_bar;
}
view raw wp_menu.php hosted with ❤ by GitHub

The above code hooks into three different components in WordPress. The first can be used to preload or render items before it shows to the user, while the second can be used to remove or load new items into the toolbar. The add_filter on the last line forces the toolbar to show for all users – by default, the toolbar is only shown to administrators.

Customising the Menu

If you want to remove any existing items from the toolbar, your code should go in the preload_my_toolbar() function. Otherwise, you can add new items in the my_toolbar() function.

Removing a node can be achieved using the remove_node() function, and passing in the node ID. Adding a new node is just as easy, using the add_node() function and passing in a list of parameters. Documentation on adding nodes can be found here. Examples of both are shown below:

<?php
// remove WordPress items from toolbar
function preload_my_toolbar() {
global $wp_admin_bar;
// remove WordPress logo
$wp_admin_bar->remove_node('wp-logo');
// remove search button
$wp_admin_bar->remove_node('search');
}
// add custom items to toolbar
function my_toolbar() {
global $wp_admin_bar;
// add a single link
$wp_admin_bar->add_node( array(
'id' => 'my-link-one',
'title' => 'Item 1',
'href' => site_url()
) );
// add a item to the right of the menu using parent: top-secondary
$wp_admin_bar->add_node( array(
'id' => 'my-search',
'title' => 'Search',
'href' => '#',
'parent' => 'top-secondary'
) );
// add a sub-item
$wp_admin_bar->add_node( array(
'id' => 'my-google-search',
'title' => 'Google',
'parent' => 'my-search',
'href' => 'http://www.google.com',
'meta' =>array( 'target' => '_blank' )
) );
// add another sub-item
$wp_admin_bar->add_node( array(
'id' => 'my-bing-search',
'title' => 'Bing',
'parent' => 'my-search',
'href' => 'http://www.bing.com',
'meta' =>array( 'target' => '_blank' )
) );
}
view raw wp_toolbar_sample.php hosted with ❤ by GitHub

Complete Code

The complete code for a working version of the plugin code uses in this post can be found here or downloaded here (gzip format). You can copy the PHP file into the /wp-content/plugins folder and activate it from the Plugins menu in the Admin Dashboard.

Install MyToolbar Plugin

Cage Cricket Toolbar

The Cage Cricket toolbar is more complicated than the version shows here, but makes use of exactly the same functions. The main differences is the addition of a custom logo replacing the default WordPress one (needs additional CSS styling to make it work), and a difference between “logged-in” and “not logged-in” states, as shown below:

“Not logged-in” View

CC Toolbar - Not Logged In

“Logged-in” View

CC Toolbar - Logged In

Cage Cricket, PHP, Tutorial, WordPress, WordPress Plugins

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Subscribe for updates

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Flat File Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux Mac OS MongoDB NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins

© 2011-2026 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy