
{"id":3542,"date":"2014-05-13T04:05:00","date_gmt":"2014-05-13T04:05:00","guid":{"rendered":"http:\/\/tapchicntt.com\/?p=3542"},"modified":"2025-04-14T09:47:40","modified_gmt":"2025-04-14T02:47:40","slug":"wordpress-cach-tao-menu-moi-tren-thanh-admin-bar","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/wordpress-cach-tao-menu-moi-tren-thanh-admin-bar\/","title":{"rendered":"[WordPress] C\u00e1ch t\u1ea1o menu m\u1edbi tr\u00ean thanh Admin Bar"},"content":{"rendered":"\n<p>M\u1eb7c \u0111\u1ecbnh thanh Admin Bar m\u00e0u \u0111en c\u1ee7a WordPress ch\u1ec9 c\u00f3 m\u1ed9t v\u00e0i \u0111\u1ed1i t\u01b0\u1ee3ng b\u00ean trong n\u00f3 nh\u01b0 link tr\u1ecf \u0111\u1ebfn Dashboard, qu\u1ea3n l\u00fd b\u00ecnh lu\u1eadn, vi\u1ebft b\u00e0i m\u1edbi.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">T\u1ed5ng quan quy tr\u00ecnh<\/h3>\n\n\n\n<p>Ch\u00fang ta s\u1ebd c\u00f3 m\u1ed9t h\u00e0m trung t\u00e2m \u0111\u1ec3 thao t\u00e1c \u0111\u00e8 l\u00ean c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng m\u1eb7c \u0111\u1ecbnh \u0111\u1ec3 th\u00eam menu. Sau \u0111\u00f3 h\u00e0m n\u00e0y s\u1ebd x\u1eed l\u00fd qua m\u1ed9t action hook c\u00f3 t\u00ean l\u00e0 <code>admin_bar_menu<\/code>, \u0111\u00e2y l\u00e0 hook \u0111\u1ec3 x\u1eed l\u00fd m\u1ecdi v\u1ea5n \u0111\u1ec1 li\u00ean quan t\u1edbi Admin Bar.<\/p>\n\n\n\n<p>\u0110\u1ec3 th\u00eam menu, ta s\u1ebd s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c c\u00f3 t\u00ean l\u00e0 add_menu v\u00e0 trong ph\u01b0\u01a1ng th\u1ee9c n\u00e0y s\u1ebd c\u00f3 m\u1ed9t v\u00e0i tham s\u1ed1 r\u1ea5t d\u1ec5 hi\u1ec3u m\u00e0 m\u00ecnh s\u1ebd tr\u00ecnh b\u00e0y k\u1ef9 h\u01a1n \u1edf ph\u1ea7n d\u01b0\u1edbi.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">B\u1eaft \u0111\u1ea7u c\u00f4ng vi\u00eac<\/h3>\n\n\n\n<p>\u0110\u1ea7u ti\u00ean ch\u00fang ta ti\u1ebfn h\u00e0nh t\u1ea1o m\u1ed9t function v\u00e0 m\u1ed9t \u0111o\u1ea1n add_action nh\u01b0 sau v\u00e0o file function.php trong theme:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action('admin_bar_menu', 'custom_admin_bar');\nfunction custom_admin_bar( $menu ) {\n        \/\/ Vi\u1ebft code v\u00e0o \u0111\u00e2y\n} \n<\/pre><\/div>\n\n\n<p>Ti\u1ebfp theo, ta s\u1eed d\u1ee5ng tham s\u1ed1 $menu th\u1ef1c thi ph\u01b0\u01a1ng th\u1ee9c add_menu v\u1edbi m\u1ed9t v\u00e0i tham s\u1ed1 nh\u01b0 sau:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$menu-&gt;add_menu( array(\n        &#039;id&#039; =&gt; &#039;menu_tccntt&#039;,\n        &#039;title&#039; =&gt; &#039;T\u1ea1p ch\u00ed CNTT&#039;,\n        &#039;href&#039; =&gt; &#039;https:\/\/tapchicntt.com&#039;,\n        &#039;meta&#039; =&gt; array( &#039;title&#039; =&gt; &#039;T\u1ea1p ch\u00ed CNTT&#039; )\n) );\n<\/pre><\/div>\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng hi\u1ec3u c\u00e1c tham s\u1ed1 b\u00ean trong, v\u00e0 code \u0111\u1ea7y \u0111\u1ee7 ta c\u00f3:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action(&#039;admin_bar_menu&#039;, &#039;custom_admin_bar&#039;);\nfunction custom_admin_bar( $menu ) {\n        $menu-&gt;add_menu( array(\n                &#039;id&#039; =&gt; &#039;menu1&#039;,\n                &#039;title&#039; =&gt; &#039;Menu 1&#039;,\n                &#039;href&#039; =&gt; &#039;http:\/\/themescss.com&#039;,\n                &#039;meta&#039; =&gt; array( &#039;title&#039; =&gt; &#039;Menu 1&#039; )\n        ) );\n} \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Ch\u00fang ta c\u00f3 k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/h2>\n\n\n\n<p>Coi nh\u01b0 l\u00e0 ta \u0111\u00e3 c\u00f3 m\u1ed9t c\u00e1i menu b\u00ecnh th\u01b0\u1eddng. N\u1ebfu b\u1ea1n mu\u1ed1n n\u00f3 l\u00e0 m\u1ed9t menu \u0111\u1ed5 xu\u1ed1ng (dropdown menu) th\u00ec sao? B\u1ea1n c\u00f3 th\u1ec3 vi\u1ebft th\u00eam m\u1ed9t \u0111o\u1ea1n <code>add_menu<\/code> n\u1eefa v\u00e0 ch\u1ee9a tham s\u1ed1 l\u00e0 ID c\u1ee7a menu m\u1eb9.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$menu-&gt;add_menu( array(\n        &#039;id&#039; =&gt; &#039;submenu1&#039;,\n        &#039;parent&#039; =&gt; &#039;menu1&#039;,\n        &#039;title&#039; =&gt; &#039;Sub Menu 1&#039;,\n        &#039;href&#039; =&gt; &#039;http:\/\/google.com&#039;,\n        &#039;meta&#039; =&gt; array(\n                &#039;title&#039; =&gt; &#039;Sub Menu 1&#039;,\n                &#039;target&#039; =&gt; &#039;blank&#039;\n        )\n) );\n<\/pre><\/div>\n\n\n<p>Trong \u0111\u00f3, b\u1ea1n \u01b0u \u00fd \u0111\u1ebfn tham s\u1ed1 parent, gi\u00e1 tr\u1ecb c\u1ee7a n\u00f3 l\u00e0 gi\u00e1 tr\u1ecb c\u1ee7a tham s\u1ed1 id c\u1ee7a menu m\u00e0 b\u1ea1n mu\u1ed1n th\u00eam sub cho n\u00f3. Gi\u1edd th\u00ec ta s\u1ebd c\u00f3:<\/p>\n\n\n\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n di chuy\u1ec3n menu ra \u0111\u1eb1ng cu\u1ed1i c\u1ee7a Admin Bar th\u00ec thay:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action('admin_bar_menu', 'custom_admin_bar');\n<\/pre><\/div>\n\n\n<p>Th\u00e0nh<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action('admin_bar_menu', 'custom_admin_bar', 100);\n<\/pre><\/div>\n\n\n<p>C\u00e1i s\u1ed1 \u0111\u1eb1ng sau l\u00e0 th\u1ee9 t\u1ef1 th\u1ef1c thi c\u1ee7a n\u00f3, s\u1ed1 c\u00e0ng nh\u1ecf th\u00ec s\u1ebd \u0111\u01b0\u1ee3c \u01b0u ti\u00ean \u0111\u1ee9ng tr\u01b0\u1edbc.<\/p>\n\n\n\n<p>\u0110\u00e2y l\u00e0 to\u00e0n b\u1ed9 code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action(&#039;admin_bar_menu&#039;, &#039;custom_admin_bar&#039;);\nfunction custom_admin_bar( $menu ) {\n        $menu-&gt;add_menu( array(\n                &#039;id&#039; =&gt; &#039;menu_tccntt&#039;,\n                &#039;title&#039; =&gt; &#039;T\u1ea1p ch\u00ed CNTT&#039;,\n                &#039;href&#039; =&gt; &#039;https:\/\/tapchicntt.com&#039;,\n                &#039;meta&#039; =&gt; array( &#039;title&#039; =&gt; &#039;T\u1ea1p ch\u00ed CNTT&#039; )\n        ) );\n        $menu-&gt;add_menu( array(\n                &#039;id&#039; =&gt; &#039;submenu1&#039;,\n                &#039;parent&#039; =&gt; &#039;menu1&#039;,\n                &#039;title&#039; =&gt; &#039;Sub Menu 1&#039;,\n                &#039;href&#039; =&gt; &#039;http:\/\/google.com&#039;,\n                &#039;meta&#039; =&gt; array(\n                        &#039;title&#039; =&gt; &#039;Sub Menu 1&#039;,\n                        &#039;target&#039; =&gt; &#039;blank&#039;\n                )\n        ) );\n}\n<\/pre><\/div>\n\n\n<p>Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>M\u1eb7c \u0111\u1ecbnh thanh Admin Bar m\u00e0u \u0111en c\u1ee7a WordPress ch\u1ec9 c\u00f3 m\u1ed9t v\u00e0i \u0111\u1ed1i t\u01b0\u1ee3ng b\u00ean trong n\u00f3 nh\u01b0 link [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":9614,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[143],"tags":[],"class_list":["post-3542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thu-thuat-wordpress"],"views":432,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/3542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=3542"}],"version-history":[{"count":7,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/3542\/revisions"}],"predecessor-version":[{"id":9624,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/3542\/revisions\/9624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/9614"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=3542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=3542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=3542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}