Add links to WordPress Toolbar

By default  WordPress displays a toolbar on top of all pages to logged in users. In this article, we will show you how to add links to WordPress toolbar.

Why You Need to Add Links to WordPress Toolbar?

By default the toolbar shows useful links to WordPress admin side, allowing users to quickly access different sections of their website. For example, links to an external resource, service, or website. These links can be added to WordPress toolbar as shortcut links allowing you and your users easy access to those locations directly from your site or the admin side.

Adding a Shortcut Link to WordPress Toolbar

Create a new file custom_toolbar_link.php in plugins folder and add the following code to the file.

 <?php 
/* 
Plugin Name: Custom Tool bar link 
Description: Add shortcut links in admin tool bar. 
Author: Gagan Deep 
*/ 	
// add a link to the WP Toolbar 	
function custom_toolbar_link($wp_admin_bar) { 		
    $args = array( 
      'id' => 'codeway',
      'title' => 'Search Codeway', 
      'href' => 'http://codeway.co.in/', 
      'meta' => array(
	'class' => 'codeway', 
	'title' => 'Search Codeway Post'
      )
   );
   $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 999);
?>

It uses the function add_node with the arguments described in the array. You need to replace the id, title, href, and meta items with values for your own custom link.

shortcut-link

 

 

Add Sub Shortcut Links in Toolbar

/*
* add a sub links under a parent link
*/

// Add a parent shortcut link
function custom_toolbar_group_link($wp_admin_bar) {
	$args = array(
	  'id' => 'codeway_posts',
	  'title' => 'Codeway', 
	  'href' => 'http://codeway.co.in/', 
	  'meta' => array(
		'class' => 'codeway', 
		'title' => 'Visit Codeway'
	  )
       );
       $wp_admin_bar->add_node($args);

      // Add the first child link 
      $args = array(
	'id' => 'codeway-request-article',
	'title' => 'Request An Article', 
	'href' => 'http://codeway.co.in/request-article/',
	'parent' => 'codeway_posts', 
	'meta' => array(
		'class' => 'codeway-request-article', 
		'title' => 'Visit Request An Article'
	)
      );
      $wp_admin_bar->add_node($args);

     // Add another child link
     $args = array(
	'id' => 'codeway-post-list',
	'title' => 'Codeway Tutorials', 
	'href' => 'http://codeway.co.in/',
	'parent' => 'codeway_posts', 
	'meta' => array(
		'class' => 'codeway-post-list', 
		'title' => 'Visit Codeway Tutorials'
	)
      );
      $wp_admin_bar->add_node($args);

      // Add a child link to the child link
      $args = array(
	'id' => 'codeway-html-css-post',
	'title' => 'Html & CSS', 
	'href' => 'http://codeway.co.in/category/html-css/',
	'parent' => 'codeway-post-list', 
	'meta' => array(
		'class' => 'codeway-html-css-post', 
		'title' => 'Visit Html & Css Tutorials on Codeway'
	)
      );
      $wp_admin_bar->add_node($args);

      // Add a child link to the child link
      $args = array(
	'id' => 'codeway-javascript-jquery-post',
	'title' => 'Javascript & Jquery', 
	'href' => 'http://codeway.co.in/category/javascript-jquery/',
	'parent' => 'codeway-post-list', 
	'meta' => array(
		'class' => 'codeway-javascript-jquery-post', 
		'title' => 'Visit Javascript & Jquery Tutorials on Codeway'
	)
      );
      $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_group_link', 999);

shortcut-sub-link

 

 

Don’t forget to share if you liked it 🙂

The following two tabs change content below.

Gagan Walia

Gagan is Senior Web Developer at Logiciel Solutions. She loves playing out with Wordpress and Cakephp.

Latest posts by Gagan Walia (see all)