How to Add Custom Shortcut Links to WordPress Toolbar

By default WordPress displays a toolbar on top of all pages to logged in users. You canΒ take control of the WordPress admin bar,Β turn it offΒ when viewing site, or even disable it forΒ all users except administrators. However, this toolbar can be utilized in many ways, particularly if you run a busy website with multiple authors. In this article, we will show you how to add custom shortcut links to WordPress toolbar.

Why or When You Need to Add Custom Shortcut Links to WordPress Toolbar?

By default the toolbar shows useful links to WordPressΒ adminstration screens, allowing users to quickly access different sections of their website.

However, everyone has links that they visit a lot when writing posts or working on their site. For example, links to an external resource, service, or website. These links can be added to WordPress toolbar as custom shortcut links allowing you and your users easy access to those locations directly from your site or theΒ admin area.

Adding a Custom Shortcut Link to WordPress Toolbar

To add a custom shortcut link to the WordPress toolbar, you need to simply copy and paste the following code in your theme’sΒ functions.phpΒ file or in aΒ site-specific plugin.

// add a link to the WP Toolbar
function custom_toolbar_link($wp_admin_bar) {
Β Β Β Β $args = array(
Β Β Β Β Β Β Β Β ‘id’ => ‘pluginthemehub’,
Β Β Β Β Β Β Β Β ‘title’ => ‘Search pluginthemehub’,
Β Β Β Β Β Β Β Β ‘href’ => ‘https://www.google.com:443/cse/publicurl?cx=014650714884974928014:oga60h37xim’,
Β Β Β Β Β Β Β Β ‘meta’ => array(
Β Β Β Β Β Β Β Β Β Β Β Β ‘class’ => ‘pluginthemehub’,
Β Β Β Β Β Β Β Β Β Β Β Β ‘title’ => ‘Search pluginthemehub Tutorials’
Β Β Β Β Β Β Β Β Β Β Β Β )
Β Β Β Β );
Β Β Β Β $wp_admin_bar->add_node($args);
}
add_action(‘admin_bar_menu’, ‘custom_toolbar_link’, 999);

This sample code adds a link to a Google Custom Search engine, which can be used to search for WordPress tutorials on Pluginthemehub. 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.

How to Add a Group of Custom Links in Toolbar

We showed you how to add a custom link to the toolbar, but what if you wanted to add multiple links and create a custom menu with handful shortcuts of your own? To do that you can group multiple shortcuts under one parent item. The child nodes under the parent link will appear when a user takes the mouse on the parent link. Here is an example of how to add a group of custom links in WordPress toolbar.

/*
* add a group of links under a parent link
*/
Β 
// Add a parent shortcut linkΒ 
Β 
function custom_toolbar_link($wp_admin_bar) {
Β  Β  $args = array(
Β  Β  Β  Β  ‘id’ => ‘pluginthemehub’,
Β  Β  Β  Β  ‘title’ => ‘pluginthemehub’,
Β  Β  Β  Β  ‘href’ => ‘https://www.pluginthemehub.com’,
Β  Β  Β  Β  ‘meta’ => array(
Β  Β  Β  Β  Β  Β  ‘class’ => ‘pluginthemehub’,
Β  Β  Β  Β  Β  Β  ‘title’ => ‘Visit pluginthemehub’
Β  Β  Β  Β  Β  Β  )
Β  Β  );
Β  Β  $wp_admin_bar->add_node($args);
Β 
Β 
// Add the first child link
Β  Β  Β 
Β  Β  $args = array(
Β  Β  Β  Β  ‘id’ => ‘pluginthemehub-guides’,
Β  Β  Β  Β  ‘title’ => ‘pluginthemehub Guides’,
Β  Β  Β  Β  ‘href’ => ‘https://www.pluginthemehub.com/category/beginners-guide/’,
Β  Β  Β  Β  ‘parent’ => ‘pluginthemehub’,
Β  Β  Β  Β  ‘meta’ => array(
Β  Β  Β  Β  Β  Β  ‘class’ => ‘pluginthemehub-guides’,
Β  Β  Β  Β  Β  Β  ‘title’ => ‘Visit WordPress pluginthemehub Guides’
Β  Β  Β  Β  Β  Β  )
Β  Β  );
Β  Β  $wp_admin_bar->add_node($args);
Β 
// Add another child link
$args = array(
Β  Β  Β  Β  ‘id’ => ‘pluginthemehub-tutorials’,
Β  Β  Β  Β  ‘title’ => ‘pluginthemehub Tutorials’,
Β  Β  Β  Β  ‘href’ => ‘https://www.pluginthemehub.com/category/wp-tutorials/’,
Β  Β  Β  Β  ‘parent’ => ‘pluginthemehub’,
Β  Β  Β  Β  ‘meta’ => array(
Β  Β  Β  Β  Β  Β  ‘class’ => ‘pluginthemehub-tutorials’,
Β  Β  Β  Β  Β  Β  ‘title’ => ‘Visit pluginthemehub Tutorials’
Β  Β  Β  Β  Β  Β  )
Β  Β  );
Β  Β  $wp_admin_bar->add_node($args);
Β 
Β 
// Add a child link to the child link
Β 
$args = array(
Β  Β  Β  Β  ‘id’ => ‘pluginthemehub-themes’,
Β  Β  Β  Β  ‘title’ => ‘pluginthemehub Themes’,
Β  Β  Β  Β  ‘href’ => ‘https://www.pluginthemehub.com/category/wp-themes/’,
Β  Β  Β  Β  ‘parent’ => ‘pluginthemehub-tutorials’,
Β  Β  Β  Β  ‘meta’ => array(
Β  Β  Β  Β  Β  Β  ‘class’ => ‘pluginthemehub-themes’,
Β  Β  Β  Β  Β  Β  ‘title’ => ‘Visit WordPress Themes Tutorials on pluginthemehub’
Β  Β  Β  Β  Β  Β  )
Β  Β  );
Β  Β  $wp_admin_bar->add_node($args);
Β 
}
Β 
add_action(‘admin_bar_menu’, ‘custom_toolbar_link’, 999);

In this example code, first we added a custom shortcut link. Next, we added another custom link and made it a child of the first link. We added the parent link id by adding the argument ‘parent’ => ‘pluginthemehub’.Β Then we repeated this to add another link under the same parent link. We have also used a child link as a parent link to show you how to add sub-items to a sub-item in the your custom links menu.

We hope this article helped you add custom link shortcuts to WordPress toolbar on your website. For questions and feedback please leave a comment comment.

What would you add as a custom shortcut link in your WordPress toolbar?

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!

Pin It on Pinterest

Add address