{"id":152192,"date":"2016-02-26T11:00:41","date_gmt":"2016-02-26T16:00:41","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=152192"},"modified":"2016-02-25T19:40:18","modified_gmt":"2016-02-26T00:40:18","slug":"wordpress-admin-toolbar","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/wordpress-admin-toolbar\/","title":{"rendered":"Customizing (or Removing) the WordPress Admin Toolbar"},"content":{"rendered":"<p>The WordPress toolbar is that thin black bar that sits above the page header of your website. It contains menus and links usually pointing to specific admin pages like the editing post pages, the user profile page, the Theme Customizer, and more.<\/p>\n<p>While the toolbar has many useful features, it can be annoying, particularly when you have no need to grant access to the backend of your site to all your users, or when you don&#8217;t necessarily consider a black rectangle aesthetically acceptable for your website.<\/p>\n<p>But <a href=\"https:\/\/codex.wordpress.org\/Toolbar\" target=\"_blank\">the toolbar<\/a> is an important part of the WordPress admin and can be extremely useful with the right customizations so you can provide your collaborators with quick access to site functionalities and specific information.<\/p>\n<p>That being said, in this article I will show you how to manage the WordPress toolbar, removing it for specific roles, adding links and menus, and customizing its appearance.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/02\/default_toolbar_full.png\" alt=\"default toolbar\" width=\"735\" height=\"32\" \/><figcaption class=\"wp-caption-text\">WordPress Toolbar<\/figcaption><\/figure>\n<h2>Removing the Admin Toolbar<\/h2>\n<p>Occasionally, you might want to remove the toolbar from the front-end of your site. You can hide it to all users or for specific roles. The following line \u2013 when added to your <em>functions.php<\/em> file (don&#8217;t forget to <a href=\"https:\/\/wpmudev.com\/blog\/how-to-create-wordpress-child-theme\/\" target=\"_blank\">use a child theme<\/a>!) \u2013 will remove the toolbar for all site users:<\/p>\n<div class=\"gist\" data-gist=\"f74484ceada00e888fe69e2e4f497a73\" data-gist-file=\"remove-toolbar2.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f74484ceada00e888fe69e2e4f497a73.js?file=remove-toolbar2.php\">Loading gist f74484ceada00e888fe69e2e4f497a73<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The\u00a0<a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/show_admin_bar\" target=\"_blank\">WordPressCodex<\/a> states that the <code>show_admin_bar<\/code> function should be called immediately upon plugin load and does not need to be called from a function hooked to the <code>init<\/code> action.<\/p>\n<p>More probably, you may want to show\/hide the toolbar depending on a\u00a0<a href=\"https:\/\/codex.wordpress.org\/Roles_and_Capabilities\" target=\"_blank\">user&#8217;s capabilities or role name<\/a>. The following code will hide the admin bar to all users but administrators and editors:<\/p>\n<div class=\"gist\" data-gist=\"1efb78d94fefeee7b7e56983ec98e934\" data-gist-file=\"remove-toolbar.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1efb78d94fefeee7b7e56983ec98e934.js?file=remove-toolbar.php\">Loading gist 1efb78d94fefeee7b7e56983ec98e934<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I said before that the <code>show_admin_bar<\/code> function does not need to be called from a callback function. So, you may ask why we&#8217;re hooking it to the <code>after_setup_theme<\/code> action? In this case, if we didn&#8217;t, WordPress would return the following error message:<\/p>\n<blockquote><p><strong>Fatal error: Call to undefined function wp_get_current_user() &#8230;<\/strong><\/p><\/blockquote>\n<p>That&#8217;s because the <code>current_user_can()<\/code> <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/current_user_can\" target=\"_blank\">function<\/a> uses <code>wp_get_current_user()<\/code> to retrieve the current user object. This latter function is not available when the plugin loads, so we have to be sure to call <code>current_user_can<\/code> not before the <code>plugins_loaded<\/code> action (check the Codex for a <a href=\"http:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference#Actions_Run_During_a_Typical_Request\" target=\"_blank\">list of typical actions<\/a> in chronological order). If you&#8217;re calling <code>current_user_can()<\/code> from within a theme&#8217;s (or child theme&#8217;s) function file, you&#8217;ll need to hook the callback function to the <code>after_setup_theme<\/code> action, instead.<\/p>\n<p>In this second example, we&#8217;re removing the toolBar for all users except administrators and editors:<\/p>\n<div class=\"gist\" data-gist=\"f5ae199d4438eb0ae9a78227cf051c1a\" data-gist-file=\"remove-toolbar3.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f5ae199d4438eb0ae9a78227cf051c1a.js?file=remove-toolbar3.php\">Loading gist f5ae199d4438eb0ae9a78227cf051c1a<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>If you want your code to be reusable, it would be preferable to hook the function to the <code>after_setup_theme<\/code> action in any case.<\/p>\n<p>Since version 3.1, WordPress has provided the <code>show_admin_bar<\/code> <a href=\"\/\/codex.wordpress.org\/Plugin_API\/Filter_Reference\/show_admin_bar\" target=\"_blank\">filter<\/a>, so we have another way to accomplish the same task. For example, we can hide the toolbar to all users with a single statement:<\/p>\n<div class=\"gist\" data-gist=\"2bf6c5be8e5e8baaa2cedd00e6606512\" data-gist-file=\"remove-toolbar4.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/2bf6c5be8e5e8baaa2cedd00e6606512.js?file=remove-toolbar4.php\">Loading gist 2bf6c5be8e5e8baaa2cedd00e6606512<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>That&#8217;s the same as the following lines:<\/p>\n<div class=\"gist\" data-gist=\"f793f2d29a3cb639c3421be3b62abe35\" data-gist-file=\"remove-toolbar5.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f793f2d29a3cb639c3421be3b62abe35.js?file=remove-toolbar5.php\">Loading gist f793f2d29a3cb639c3421be3b62abe35<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>And we can show\/hide the toolbar depending on a\u00a0user&#8217;s capabilities, as well:<\/p>\n<div class=\"gist\" data-gist=\"95ff1b0ea891578d96d217e3252c5c47\" data-gist-file=\"remove-toolbar6.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/95ff1b0ea891578d96d217e3252c5c47.js?file=remove-toolbar6.php\">Loading gist 95ff1b0ea891578d96d217e3252c5c47<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>In this example, the toolbar will be shown only to administrators and editors (they can <code>publish_posts<\/code>).<\/p>\n<p>That&#8217;s all that we have to know when we decide to remove the toolbar. But what if we wanted to use the Toolbar to give new powers to our WordPress install?<\/p>\n<h2>Customizing the Admin Toolbar<\/h2>\n<p>The <code>WP_Admin_Bar<\/code> <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Admin_Bar\" target=\"_blank\">Class<\/a>\u00a0controls the toolbar. Thanks to the class methods we can add and remove menu elements (nodes) and groups of elements.<\/p>\n<p>In our examples we&#8217;re going to use just the following three methods:<\/p>\n<ul>\n<li><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/add_node\" target=\"_blank\">add_node()<\/a><\/li>\n<li><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/add_group\" target=\"_blank\">add_group()<\/a><\/li>\n<li><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/get_node\" target=\"_blank\">get_node()<\/a><\/li>\n<\/ul>\n<p>Default menus are defined in <a href=\"https:\/\/core.trac.wordpress.org\/browser\/tags\/4.4.2\/src\/wp-includes\/admin-bar.php\" target=\"_blank\">\/wp-includes\/admin-bar.php<\/a>. Some of them are available for all logged-in users, like the <em>WordPress Logo<\/em> menu (which shows some institutional links), the\u00a0<em>My Account<\/em> menu (which shows some links related to the current user), and <em>Site Name<\/em> menu (which provides quick links to the admin panel).<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/02\/site_name_menu.png\" alt=\"Site name menu\" width=\"260\" height=\"177\" \/><figcaption class=\"wp-caption-text\">Site name menu<\/figcaption><\/figure>\n<p>But WordPress gives us the ability to add custom menus and links, textual information and form fields. I won&#8217;t explain here how to add items to the toolbar, as we&#8217;ve already covered this topic in <a href=\"https:\/\/wpmudev.com\/blog\/wordpress-toolbar\/\" target=\"_blank\">How to Add Items to the WordPress Toolbar<\/a>. Rather, I will show you two practical examples of customization, starting with an easy upgrade of the built-in <em>My Account<\/em> menu.<\/p>\n<h2>How to add new items to an existing menu<\/h2>\n<p>When our goal is adding nodes to the toolbar depending on the user&#8217;s role, we have to define a callback function keeping as argument an instance of the <code>WP_Admin_Bar<\/code> object. The function should be hooked to the <code>admin_bar_menu<\/code> action as shown in the following code:<\/p>\n<div class=\"gist\" data-gist=\"338bed2cc28fe64d91e403632b87da83\" data-gist-file=\"customize-toolbar.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/338bed2cc28fe64d91e403632b87da83.js?file=customize-toolbar.php\">Loading gist 338bed2cc28fe64d91e403632b87da83<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I mentioned earlier in this article that we can build new menus as well as adding links to existing menus. In this example, we&#8217;re going to provide the current user with a quick link to his\/her personal website, adding a new node to the\u00a0<em>My Account<\/em> built-in menu.<\/p>\n<p>When the <em>admin-bar.php<\/em> loads, a new group of nodes named <code>user-actions<\/code> is added to the\u00a0<code>my-account<\/code> menu. This group of nodes will be considered a parent for any new custom link we adding to the menu. The following code will add the link to the group:<\/p>\n<div class=\"gist\" data-gist=\"caa29faafedbc11d9b2b59e4f893a7b0\" data-gist-file=\"customize-toolbar2.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/caa29faafedbc11d9b2b59e4f893a7b0.js?file=customize-toolbar2.php\">Loading gist caa29faafedbc11d9b2b59e4f893a7b0<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>First, we get the <code>$current_user<\/code> object and check if it is a valid instance of <code>WP_User<\/code>. Then we get the <code>my-account<\/code> node object, which corresponds to the <em>My Account<\/em> menu placed on the right side of the Toolbar. Finally, if the <code>user_url<\/code> meta field and the node object exist, then we add the <code>user-url<\/code> node to the menu.<\/p>\n<p>The code above will generate the following mark-up:<\/p>\n<div class=\"gist\" data-gist=\"7fc4609a8bc8ae3d629c62f3a50828be\" data-gist-file=\"toolbar-node.htm\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/7fc4609a8bc8ae3d629c62f3a50828be.js?file=toolbar-node.htm\">Loading gist 7fc4609a8bc8ae3d629c62f3a50828be<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The resulting menu is shown in the image below.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/02\/my_account.png\" alt=\"The default menu at the top compared to the customized &lt;em&gt;My Account&lt;\/em&gt; menu in the toolbar\" width=\"312\" height=\"343\" \/><figcaption class=\"wp-caption-text\">The default menu at the top compared to the customized <em>My Account<\/em> menu in the toolbar<\/figcaption><\/figure>\n<h2>An Advanced Example: Conditional Menus, Custom Post Types and More<\/h2>\n<p>Some Toolbar menus are available only in specific pages. For instance, the <em>Edit post<\/em> menu, which provides a quick link to the editing page of the current post or taxonomy term, appears exclusively in single post pages and taxonomy archives.<\/p>\n<p>This may suggest to us the idea of showing up menus under specific conditions. In the following example, the condition will be provided by the user role.<\/p>\n<p>So, we may like to show to the site editors a role-specific menu holding a group of links pointing to the admin screens of pending posts (one link for each post type). This sort of menu would be extremely useful for multi-author websites when many users write posts (and custom post types) expecting to be reviewed for publication.<\/p>\n<p>Now, let&#8217;s get back to our callback function and add the following code:<\/p>\n<div class=\"gist\" data-gist=\"84c204e267027987984c01a6412f8258\" data-gist-file=\"customize-toolbar3.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/84c204e267027987984c01a6412f8258.js?file=customize-toolbar3.php\">Loading gist 84c204e267027987984c01a6412f8258<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>In the code above, first, we check whether the current user is an editor. If he\/she is, we add the <code>editor-menu<\/code> top-level node (no parent for this node). Later, we add the <code>editor-actions<\/code> group, setting the <code>editor-menu<\/code> item as its parent node.<\/p>\n<p>Now comes the funny part. The <code>get_post_types<\/code> function retrieves from the database an array of custom post type objects (more in the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/get_post_types\" target=\"_blank\">Codex<\/a>). For each post type, we check if the logged-in user has the role of editor (she can <code>publish_posts<\/code>). Then <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/get_posts\" target=\"_blank\">we get an array<\/a> of all pending posts in the current post type and count them.<\/p>\n<p>Finally, for each post type, we add a node to the <code>editor-actions<\/code> group. Each link will point to the pending post type screen.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/02\/editor_menu.png\" alt=\"The Pending Posts menu will be available only to site editors\" width=\"242\" height=\"161\" \/><figcaption class=\"wp-caption-text\">The Pending Posts menu will be available only to site editors<\/figcaption><\/figure>\n<p>And, if you&#8217;d like to customize the menu presentation with a gorgeous icon from <a href=\"https:\/\/developer.wordpress.org\/resource\/dashicons\/\" target=\"_blank\">Dashicon<\/a> set, just append the following code to your plugin or <em>functions.php<\/em> file:<\/p>\n<div class=\"gist\" data-gist=\"4a1ed7bb56005bc6f75f043d8411f1f1\" data-gist-file=\"menu-styling.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4a1ed7bb56005bc6f75f043d8411f1f1.js?file=menu-styling.php\">Loading gist 4a1ed7bb56005bc6f75f043d8411f1f1<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The function we&#8217;ve hooked to the <code>wp_head<\/code> <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/wp_head\" target=\"_blank\">action<\/a> just prints a style element within the <code>head<\/code> of the document. Sure, this is not a best practice when you enqueue a style in a document, but here I assume that we just need one style declaration. Loading a whole CSS file would not be an efficient option.<\/p>\n<p>But if you want to merge the toolbar into the look and feel of your website, you will have to overwrite the styles declared in <em>\/wp-includes\/css\/admin-bar.css<\/em>, and enqueue your stylesheets the right way, as well explained in <a href=\"https:\/\/wpmudev.com\/blog\/adding-scripts-and-styles-wordpress-enqueueing\/\" target=\"_blank\">Adding Scripts and Styles to WordPress the Right Way With Enqueueing<\/a>.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Left as is, the toolbar might seem like a necessary and unsightly blight at the top of your website. But when you consider its possible uses and the potential for customization, it quickly becomes apparent how much of a useful and flexible tool the toolbar can be, both for site owners and all users who collaborate on a website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While many site owners are quick to disable the WordPress admin toolbar that sits at the top of WordPress websites, it actually has many useful features \u2013\u00a0and more so with the right customizations.<\/p>\n","protected":false},"author":387958,"featured_media":152465,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[557,263],"tags":[10211,8822],"tutorials_categories":[],"class_list":["post-152192","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-tutorials","tag-admin","tag-toolbar"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/152192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/387958"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=152192"}],"version-history":[{"count":59,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/152192\/revisions"}],"predecessor-version":[{"id":190371,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/152192\/revisions\/190371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/152465"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=152192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=152192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=152192"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=152192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}