{"id":50983,"date":"2011-02-28T19:10:09","date_gmt":"2011-02-28T19:10:09","guid":{"rendered":"http:\/\/barrycarlyon.co.uk\/wordpress\/?p=50983"},"modified":"2011-02-28T19:25:01","modified_gmt":"2011-02-28T19:25:01","slug":"wordpress-and-the-admin-bar","status":"publish","type":"post","link":"https:\/\/barrycarlyon.co.uk\/wordpress\/2011\/02\/28\/wordpress-and-the-admin-bar\/","title":{"rendered":"WordPress and the Admin Bar"},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2010\/04\/wp.jpg\" rel=\"attachment wp-att-282\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2010\/04\/wp-300x300.jpg?resize=100%2C100\" alt=\"\" title=\"wp\" width=\"100\" height=\"100\" class=\"alignright size-medium wp-image-282\" srcset=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2010\/04\/wp.jpg?resize=300%2C300&amp;ssl=1 300w, https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2010\/04\/wp.jpg?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2010\/04\/wp.jpg?resize=50%2C50&amp;ssl=1 50w, https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2010\/04\/wp.jpg?w=500&amp;ssl=1 500w\" sizes=\"auto, (max-width: 100px) 85vw, 100px\" \/><\/a><\/p>\n<p>So <a href=\"http:\/\/wordpress.org\/news\/2011\/02\/threeone\/\">WordPress 3.1<\/a> introduced the new admin bar. Essentially its another way to navigate around your WordPress Blog&#8217;s inner workings, the bar itself can be enabled and disabled both inside and outside the admin system, for I like to have it enabled on both the admin and non admin sides of WordPress.<\/p>\n<p>The <a href=\"http:\/\/wordpress.org\/extend\/plugins\/stats\/\">WordPress.com Stats Plugin<\/a> makes quite good use, on the non admin side, by showing a graph of site visits over the last 48hours:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/wordpress_dot_com_stats_admin_bar.jpg\" rel=\"attachment wp-att-50984\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/wordpress_dot_com_stats_admin_bar.jpg?resize=501%2C157\" alt=\"\" title=\"wordpress_dot_com_stats_admin_bar\" width=\"501\" height=\"157\" class=\"aligncenter size-full wp-image-50984\" srcset=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/wordpress_dot_com_stats_admin_bar.jpg?w=501&amp;ssl=1 501w, https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/wordpress_dot_com_stats_admin_bar.jpg?resize=300%2C94&amp;ssl=1 300w\" sizes=\"auto, (max-width: 501px) 85vw, 501px\" \/><\/a><\/p>\n<p>This led me to think about how to put my own links on, so I turned to adding the navigation from <a href=\"http:\/\/yourmembers.co.uk\/#af:1001\">YourMembers<\/a> to the admin bar (but in this case on Both Sides, admin and non admin).<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/yourmembers_admin_bar.jpg\" rel=\"attachment wp-att-50985\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/yourmembers_admin_bar.jpg?resize=840%2C217\" alt=\"\" title=\"yourmembers_admin_bar\" width=\"840\" height=\"217\" class=\"aligncenter size-full wp-image-50985\" srcset=\"https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/yourmembers_admin_bar.jpg?w=1130&amp;ssl=1 1130w, https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/yourmembers_admin_bar.jpg?resize=300%2C77&amp;ssl=1 300w, https:\/\/i0.wp.com\/barrycarlyon.co.uk\/wordpress\/wp-content\/uploads\/2011\/02\/yourmembers_admin_bar.jpg?resize=1024%2C264&amp;ssl=1 1024w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/p>\n<p>Won&#8217;t show you a admin side one as it looks exactly the same \ud83d\ude1b<\/p>\n<p>Actually writing the code to make the <a href=\"http:\/\/yourmembers.co.uk\/#af:1001\">YourMembers<\/a> navigation be added to the nav bar, is actually relatively simple, since the admin bar itself has been written rather nicely in its own class.<\/p>\n<p>Opening up the admin-bar.php file in wp-includes is a bit messy, but looking at the first function (_wp_admin_bar_init) shows the class function add_menus() and further down add_menu, so off I went to find them&#8230;. A quick grep later finds that is class-wp-admin-bar.php and from there just a matter of reading what add_menu wants&#8230;.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n\tfunction add_menu( $args = array() ) {\r\n\t\t$defaults = array(\r\n\t\t\t&#039;title&#039; =&gt; false,\r\n\t\t\t&#039;href&#039; =&gt; false,\r\n\t\t\t&#039;parent&#039; =&gt; false, \/\/ false for a root menu, pass the ID value for a submenu of that menu.\r\n\t\t\t&#039;id&#039; =&gt; false, \/\/ defaults to a sanitized title value.\r\n\t\t\t&#039;meta&#039; =&gt; false \/\/ array of any of the following options: array( &#039;html&#039; =&gt; &#039;&#039;, &#039;class&#039; =&gt; &#039;&#039;, &#039;onclick&#039; =&gt; &#039;&#039;, target =&gt; &#039;&#039;, title =&gt; &#039;&#039; );\r\n\t\t);\r\n<\/pre>\n<p>I went thru and checked to see where the other &#8220;normal admin&#8221; links were being added, the function add_menus in the same class file shows that. The key thing to be aware of is the use of priority on those do_actions (line 182 thru 194) in order to order the links, so needing\/wanting the YourMembers links to be at the end I went for a sensible priority of 90 since the last normal admin was set to 80.<\/p>\n<p>Then it was just a matter of setting up my own add_action on the same hook (&#8216;admin_bar_menu&#8217;) and then globalising the pre existing class method and adding my menus to it.<\/p>\n<p>Since it is a drop down menu, needed to add the top menu as a parent and then adding the child nodes.<\/p>\n<p>Its quite a clever class since the class function of add_menu can handle both parents and childs at the same time with little fuss, as long as you pass it the right ID variables.<\/p>\n<p>Heres a copy of my function:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\nfunction ym_admin_bar() {\r\n\tglobal $wp_admin_bar;\r\n\r\n\tif (ym_admin_user_has_access(TRUE)) {\r\n\t\t$wp_admin_bar-&gt;add_menu(\r\n\t\t\tarray(\r\n\t\t\t\t&#039;id&#039;\t\t=&gt; &#039;yourmembers_adb&#039;,\r\n\t\t\t\t&#039;title&#039;\t\t=&gt; &#039;Your Members&#039;,\r\n\t\t\t\t&#039;href&#039;\t\t=&gt; YM_ADMIN_INDEX_URL\r\n\t\t\t)\r\n\t\t);\r\n\t\tglobal $ym_nav;\r\n\t\tforeach ($ym_nav as $page =&gt; $subpages) {\r\n\t\t\tif (is_array($subpages)) {\r\n\t\t\t\t$first = array_shift($subpages);\r\n\t\t\t} else {\r\n\t\t\t\t$first = &#039;&#039;;\r\n\t\t\t}\r\n\t\t\t$id = &#039;ym_adb_&#039; . strtolower($first);\r\n\t\t\t$url = strtolower($first);\r\n\t\t\tif (substr($first, 0, 5) == &#039;other&#039;) {\r\n\t\t\t\t$url = &#039;ym-other&amp;action=&#039; . substr($first, 6);\r\n\t\t\t}\r\n\t\t\t$wp_admin_bar-&gt;add_menu(\r\n\t\t\t\tarray(\r\n\t\t\t\t\t&#039;parent&#039;\t=&gt; &#039;yourmembers_adb&#039;,\r\n\t\t\t\t\t&#039;id&#039;\t\t=&gt; $id,\r\n\t\t\t\t\t&#039;title&#039;\t\t=&gt; $page,\r\n\t\t\t\t\t&#039;href&#039;\t\t=&gt; YM_ADMIN_INDEX_URL . &#039;&amp;ym_page=&#039; . $url\r\n\t\t\t\t)\r\n\t\t\t);\r\n\t\t}\r\n\t}\r\n\treturn;\r\n}\r\nadd_action(&#039;admin_bar_menu&#039;, &#039;ym_admin_bar&#039;, 90);\r\n?&gt;\r\n<\/pre>\n<p>There are a couple of funky bits in there due to the method we currently generate the nav items.<br \/>\nThe essential bit to consider is the array passed to $wp_admin_bar->add_menu at the start (the parent element note, no parent key) and the $wp_admin_bar->add_menu at the end, specifying a child element.<\/p>\n<p>The function ym_admin_user_has_access just checks to see if the user is of the correct level or not.<\/p>\n<p>You might want to throw in a switch on <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/is_admin\">is_admin()<\/a> to return different navigational items depending on if you are in the admin or not.<\/p>\n<p>I&#8217;m sure there is a lot more you can do with the class function but when I installed <a href=\"http:\/\/wordpress.org\/news\/2011\/02\/threeone\/\">WordPress 3.1<\/a> and saw the admin bar and WordPress.com Site stats this was the first thing I thought of and implemented, tho why its taken me this long to blog about it I don&#8217;t know&#8230;..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So WordPress 3.1 introduced the new admin bar. Essentially its another way to navigate around your WordPress Blog&#8217;s inner workings, the bar itself can be enabled and disabled both inside and outside the admin system, for I like to have it enabled on both the admin and non admin sides of WordPress. The WordPress.com Stats &hellip; <a href=\"https:\/\/barrycarlyon.co.uk\/wordpress\/2011\/02\/28\/wordpress-and-the-admin-bar\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;WordPress and the Admin Bar&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[88,45,178,177],"tags":[202,201,203,135,176],"class_list":["post-50983","post","type-post","status-publish","format-standard","hentry","category-code-geekery","category-geekery","category-wordpress","category-ym","tag-3-1","tag-admin-bar","tag-navigation","tag-wordpress-tag","tag-yourmembers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/50983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/comments?post=50983"}],"version-history":[{"count":0,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/50983\/revisions"}],"wp:attachment":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/media?parent=50983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/categories?post=50983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/tags?post=50983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}