How to Add Custom Shortcut Links to WordPress Toolbar

///How to Add Custom Shortcut Links to WordPress Toolbar

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.

01 // add a link to the WP Toolbar
02 function custom_toolbar_link($wp_admin_bar) {
03     $args = array(
04         'id' => 'wpbeginner',
05         'title' => 'Search WPBeginner',
06         'href' => 'https://www.google.com:443/cse/publicurl?cx=014650714884974928014:oga60h37xim',
07         'meta' => array(
08             'class' => 'wpbeginner',
09             'title' => 'Search WPBeginner Tutorials'
10             )
11     );
12     $wp_admin_bar->add_node($args);
13 }
14 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 WPBeginner. 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.

customlink-wptoolbar

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.

01 /*
02 * add a group of links under a parent link
03 */
04
05 // Add a parent shortcut link
06
07 function custom_toolbar_link($wp_admin_bar) {
08     $args = array(
09         'id' => 'wpbeginner',
10         'title' => 'WPBeginner',
11         'href' => 'https://www.wpbeginner.com',
12         'meta' => array(
13             'class' => 'wpbeginner',
14             'title' => 'Visit WPBeginner'
15             )
16     );
17     $wp_admin_bar->add_node($args);
18
19 // Add the first child link
20     
21     $args = array(
22         'id' => 'wpbeginner-guides',
23         'title' => 'WPBeginner Guides',
24         'href' => 'http://www.wpbeginner.com/category/beginners-guide/',
25         'parent' => 'wpbeginner',
26         'meta' => array(
27             'class' => 'wpbeginner-guides',
28             'title' => 'Visit WordPress Beginner Guides'
29             )
30     );
31     $wp_admin_bar->add_node($args);
32
33 // Add another child link
34 $args = array(
35         'id' => 'wpbeginner-tutorials',
36         'title' => 'WPBeginner Tutorials',
37         'href' => 'http://www.wpbeginner.com/category/wp-tutorials/',
38         'parent' => 'wpbeginner',
39         'meta' => array(
40             'class' => 'wpbeginner-tutorials',
41             'title' => 'Visit WPBeginner Tutorials'
42             )
43     );
44     $wp_admin_bar->add_node($args);
45
46 // Add a child link to the child link
47
48 $args = array(
49         'id' => 'wpbeginner-themes',
50         'title' => 'WPBeginner Themes',
51         'href' => 'http://www.wpbeginner.com/category/wp-themes/',
52         'parent' => 'wpbeginner-tutorials',
53         'meta' => array(
54             'class' => 'wpbeginner-themes',
55             'title' => 'Visit WordPress Themes Tutorials on WPBeginner'
56             )
57     );
58     $wp_admin_bar->add_node($args);
59
60 }
61
62 add_action('admin_bar_menu', 'custom_toolbar_link', 999);

customlinksgroup-toolbar

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' => 'wpbeginner'. 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?

Content Source