WordPress is configured to display a toolbar at the top of the page to all logged in users by default. This toolbar provides a useful way for users to navigate through different areas of your site’s back end. However, some users might want custom shortcuts in WordPress admin bar, in order to quickly direct their users to different areas of the site.
Why add custom shortcuts in WordPress Admin Bar?
The WordPress admin bar will display links to frequently used administration screens, such as links to the Dashboard, new page or post editor, menus, etc.
Those links are useful to nearly everyone who runs a WordPress site. However, there are other links, which may be equally useful. For example, you might want quick links to news websites that your editorial staff frequently references or other areas on your site that might not be included in the standard admin bar links.
Add Custom Shortcuts in WordPress Admin Bar
In your child theme’s functions.php file, add the following code:
// add custom shortcut in the WordPress admin bar function custom_shortcut_admin_bar($wp_shortcut) { $args = array( 'id' => 'wordpressorg', 'title' => 'Visit WordPress.org', 'href' => 'https://www.wordpress.org', 'meta' => array( 'class' => 'wordpressorg', 'title' => 'Visit WordPress.org' ) ); $wp_shortcut->add_node($args); } add_action('admin_bar_menu', 'custom_shortcut_admin_bar', 999); |
For your site, you will have to update the id, title, href, class, and title elements to whatever you need.
This code generates a link like this:
Drop-Down Multiple Custom Shortcuts
Let’s say that instead of just linking to WordPress.org from the WordPress admin bar, we want to link to the WordPress themes, plugins, support forum, and a sub-link to the Plugins and Hacks forum pages. It would be nice if those options dropped down under the current WordPress.org link, wouldn’t it?
To get this drop-down menu, we will expand upon the first code above.
// add custom shortcut drop-down in the WordPress admin bar function custom_shortcut_admin_bar($wp_shortcut) { // add the shortcut to WordPress.org $args = array( 'id' => 'wordpressorg', 'title' => 'Visit WordPress.org', 'href' => 'https://www.wordpress.org', 'meta' => array( 'class' => 'wordpressorg', 'title' => 'Visit WordPress.org' ) ); $wp_shortcut->add_node($args); // add the first child shortcut to WordPress themes page $args = array( 'id' => 'wordpressorg-themes', 'title' => 'WordPress Themes', 'href' => 'https://www.wordpress.org/themes', 'parent' => 'wordpressorg', 'meta' => array( 'class' => 'wordpressorg-themes', 'title' => 'Visit WordPress.org Themes' ) ); $wp_shortcut->add_node($args); // add the second child shortcut to WordPress plugins page $args = array( 'id' => 'wordpressorg-plugins', 'title' => 'WordPress Plugins', 'href' => 'https://www.wordpress.org/plugins', 'parent' => 'wordpressorg', 'meta' => array( 'class' => 'wordpressorg-plugins', 'title' => 'Visit WordPress.org Plugins' ) ); $wp_shortcut->add_node($args); // add the third child shortcut to WordPress support forum page $args = array( 'id' => 'wordpressorg-forum', 'title' => 'WordPress Support Forum', 'href' => 'https://www.wordpress.org/support', 'parent' => 'wordpressorg', 'meta' => array( 'class' => 'wordpressorg-forum', 'title' => 'Visit WordPress.org Support' ) ); $wp_shortcut->add_node($args); // add a child shortcut under the WordPress support forum page to the Plugins and Hacks forum $args = array( 'id' => 'wordpressorg-forum-plugins-hacks', 'title' => 'Plugins and Hacks', 'href' => 'https://wordpress.org/support/forum/plugins-and-hacks', 'parent' => 'wordpressorg-forum', 'meta' => array( 'class' => 'wordpressorg-forum-plugins-hacks', 'title' => 'Visit WordPress.org Support Plugins and Hacks' ) ); $wp_shortcut->add_node($args); } add_action('admin_bar_menu', 'custom_shortcut_admin_bar', 999); |
Using the code above, we created the parent shortcut the same way we created the single link to WordPress.org originally. For each of the child shortcuts, we added a new 'parent'
argument, which references the 'id'
of the parent. In the case of the shortcuts to the Themes, Plugins, and Support Forum, the parent id was 'wordpressorg'
. When we decided to add another level to the menu, the 'parent'
argument we referenced referred to the id 'wordpressorg-forum'
, as the WordPress Support Forum was the next level up on our menu.
Disable WordPress Admin Bar
On some occasions, it might be necessary to disable the WordPress admin bar for all users except for administrators, or perhaps you might want to disable it altogether.
Once again we’re working in your child theme’s functions.php file.
To disable the WordPress admin bar for all users except for users with administrative privileges insert the following code:
add_action('after_setup_theme', 'disable_admin_bar'); function disable_admin_bar() { if ( ! current_user_can( 'manage_options' ) ) { show_admin_bar( false ); } } |
To disable the admin bar for everyone including administrators, add this code instead:
show_admin_bar( false ); |
You can also hide the WordPress admin bar on the front facing pages, while keeping it on the admin pages (dashboard, etc.) with:
add_filter('show_admin_bar', '__return_false'); |
Leave a Reply