{"id":46649,"date":"2026-10-10T17:17:33","date_gmt":"2026-10-10T17:17:33","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46649"},"modified":"2026-10-10T17:17:33","modified_gmt":"2026-10-10T17:17:33","slug":"wordpress-admin-bar-shortcuts","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-admin-bar-shortcuts\/","title":{"rendered":"Add Admin Shortcuts to the WordPress Admin Bar"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The WordPress admin bar is the one menu you see both on the front end and in wp-admin, yet the screens you open most, Plugins, Themes and Updates, are two or three clicks away from the front end. This snippet adds a single <strong>Shortcuts<\/strong> menu to the admin bar with those links, plus a link that opens a new Scripts Organizer code block.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each link only appears for users who are allowed to use that screen, so editors and shop managers never see a link that ends on a &#8220;Sorry, you are not allowed&#8221; page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Shortcuts menu in the admin bar.\n * Each link is shown only to users who can open its destination.\n *\/\nadd_action( 'admin_bar_menu', 'dpc_admin_bar_shortcuts', 100 );\n\nfunction dpc_admin_bar_shortcuts( WP_Admin_Bar $admin_bar ): void {\n\t$links = array();\n\n\t$code_blocks = get_post_type_object( 'scorg' );\n\tif ( $code_blocks &amp;&amp; current_user_can( $code_blocks-&gt;cap-&gt;create_posts ) ) {\n\t\t$links&#91;'dpc-shortcut-code-block'&#93; = array(\n\t\t\t'title' =&gt; __( 'New Code Block', 'dpc' ),\n\t\t\t'url'   =&gt; admin_url( 'post-new.php?post_type=scorg' ),\n\t\t);\n\t}\n\n\t\/\/ Same check WordPress uses for the Updates screen.\n\tif ( current_user_can( 'update_core' ) || current_user_can( 'update_plugins' ) || current_user_can( 'update_themes' ) || current_user_can( 'update_languages' ) ) {\n\t\t$links&#91;'dpc-shortcut-updates'&#93; = array(\n\t\t\t'title' =&gt; __( 'Updates', 'dpc' ),\n\t\t\t'url'   =&gt; is_multisite() ? network_admin_url( 'update-core.php' ) : admin_url( 'update-core.php' ),\n\t\t);\n\t}\n\n\tif ( current_user_can( 'activate_plugins' ) ) {\n\t\t$links&#91;'dpc-shortcut-plugins'&#93; = array(\n\t\t\t'title' =&gt; __( 'Plugins', 'dpc' ),\n\t\t\t'url'   =&gt; admin_url( 'plugins.php' ),\n\t\t);\n\t}\n\n\tif ( current_user_can( 'switch_themes' ) ) {\n\t\t$links&#91;'dpc-shortcut-themes'&#93; = array(\n\t\t\t'title' =&gt; __( 'Themes', 'dpc' ),\n\t\t\t'url'   =&gt; admin_url( 'themes.php' ),\n\t\t);\n\t}\n\n\tif ( empty( $links ) ) {\n\t\treturn;\n\t}\n\n\t$admin_bar-&gt;add_node(\n\t\tarray(\n\t\t\t'id'    =&gt; 'dpc-shortcuts',\n\t\t\t'title' =&gt; esc_html__( 'Shortcuts', 'dpc' ),\n\t\t)\n\t);\n\n\t\/\/ The admin bar escapes href itself; titles are printed as HTML, so escape them here.\n\tforeach ( $links as $id =&gt; $link ) {\n\t\t$admin_bar-&gt;add_node(\n\t\t\tarray(\n\t\t\t\t'parent' =&gt; 'dpc-shortcuts',\n\t\t\t\t'id'     =&gt; $id,\n\t\t\t\t'title'  =&gt; esc_html( $link&#91;'title'&#93; ),\n\t\t\t\t'href'   =&gt; $link&#91;'url'&#93;,\n\t\t\t)\n\t\t);\n\t}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>admin_bar_menu<\/code> passes the <code>WP_Admin_Bar<\/code> object to <code>dpc_admin_bar_shortcuts()<\/code>. Priority <code>100<\/code> runs after the core items, so the menu sits at the end of the bar.<\/li>\n\n\n<li>Every link is gated by the capability WordPress itself checks on that screen: <code>activate_plugins<\/code> for Plugins, <code>switch_themes<\/code> for Themes, and for Updates any of <code>update_core<\/code>, <code>update_plugins<\/code>, <code>update_themes<\/code> or <code>update_languages<\/code>, the same check as the Updates screen.<\/li>\n\n\n<li>The New Code Block link asks the <code>scorg<\/code> post type which capability creates a code block (<code>$code_blocks-&gt;cap-&gt;create_posts<\/code>), so it follows Scripts Organizer&#8217;s own permissions. When Scripts Organizer is not active the post type does not exist and the link is skipped.<\/li>\n\n\n<li>On multisite the Updates link points to the network admin, where updates are managed.<\/li>\n\n\n<li>Titles go through <code>esc_html()<\/code>, because the admin bar prints node titles as HTML. The parent node has no link, so it opens the dropdown on touch screens too.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Compared with the older One-Click Import version, the Oxygen and Code Snippets groups are gone, <code>is_plugin_active()<\/code> and the include of <code>wp-admin\/includes\/plugin.php<\/code> are no longer needed, and each link has its own capability check instead of one <code>manage_options<\/code> check for everything.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. The admin bar shows on the front end as well as in wp-admin, so <strong>Admin only<\/strong> would hide the menu on the site itself. <code>current_user_can()<\/code> is only called inside the <code>admin_bar_menu<\/code> callback, after WordPress has loaded the current user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a link: append another entry to <code>$links<\/code> with an id starting with <code>dpc-shortcut-<\/code>, a <code>title<\/code> and a <code>url<\/code>, wrapped in the capability check that screen uses. The admin bar escapes <code>href<\/code> itself; keep <code>esc_html()<\/code> on titles because they are printed as HTML.<\/li>\n\n\n<li>Show the menu only on the front end: add <code>if ( is_admin() ) { return; }<\/code> as the first line of <code>dpc_admin_bar_shortcuts()<\/code>.<\/li>\n\n\n<li>Promote a link to the top level: remove the <code>parent<\/code> key from that node.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For one-off links, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-link-inside-wp-admin-topbar\/\">Add link inside WP-Admin topbar<\/a> and <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-media-link-to-admin-bar\/\">Add Media Link To Admin Bar<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scripts Organizer has its own admin bar menu that lists every code block. Turn it on with <strong>Code Blocks in admin bar<\/strong> on the Scripts Organizer <strong>Features<\/strong> page. This snippet only adds the New Code Block link, so the two do not overlap.<\/li>\n\n\n<li>The admin bar only renders on the front end when <strong>Show Toolbar when viewing site<\/strong> is ticked in the user&#8217;s profile.<\/li>\n\n\n<li>Node ids must be unique across the whole bar. The <code>dpc-<\/code> prefix keeps them clear of core ids such as <code>updates<\/code> and <code>themes<\/code>.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Add a Shortcuts menu to the WordPress admin bar with Plugins, Themes, Updates and New Code Block links, each shown only to users who can use it.<\/p>\n","protected":false},"author":1,"featured_media":46650,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Add Admin Shortcuts to the WordPress Admin Bar","description":"Add a Shortcuts menu to the WordPress admin bar with Plugins, Themes, Updates and New Code Block links, each shown only to users who can use it."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[199,176,166,6],"dpc_library":[204],"class_list":["post-46649","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-admin-bar","tag-capabilities","tag-scripts-organizer","tag-wp-admin"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46649"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46649\/revisions"}],"predecessor-version":[{"id":46652,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46649\/revisions\/46652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46650"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46649"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46649"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}