{"id":9951,"date":"2021-04-15T20:16:25","date_gmt":"2021-04-15T20:16:25","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/?post_type=code_snippets&#038;p=9951"},"modified":"2026-10-10T20:06:07","modified_gmt":"2026-10-10T20:06:07","slug":"color-admin-menu-items","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/color-admin-menu-items\/","title":{"rendered":"Color WordPress Admin Menu Items"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A long WordPress admin menu is hard to scan. Giving the items you use every day their own colour makes them easy to find. This snippet colours chosen WordPress admin menu items, background and text, from one config array.<\/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 * Colour chosen items in the WordPress admin menu.\n *\/\n\n\/\/ ===== Config: menu item ID =&gt; background and text colour (hex) =====\nfunction dpc_admin_menu_colors() {\n\t$items = array(\n\t\t'menu-posts' =&gt; array( 'background' =&gt; '#eede01', 'color' =&gt; '#000000' ),\n\t\t'menu-media' =&gt; array( 'background' =&gt; '#00c69e', 'color' =&gt; '#ffffff' ),\n\t\t\/\/ 'menu-posts-download'                          =&gt; array( 'background' =&gt; '#eede01', 'color' =&gt; '#000000' ), \/\/ Easy Digital Downloads\n\t\t\/\/ 'toplevel_page_edit-post_type-acf-field-group' =&gt; array( 'background' =&gt; '#00c69e', 'color' =&gt; '#ffffff' ), \/\/ ACF\n\t);\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_admin_menu_colors', $items );\n}\n\nfunction dpc_admin_menu_colors_css() {\n\t$css = '';\n\n\tforeach ( dpc_admin_menu_colors() as $id =&gt; $colors ) {\n\t\t$id    = sanitize_html_class( $id );\n\t\t$bg    = sanitize_hex_color( $colors&#91;'background'&#93; ?? '' );\n\t\t$color = sanitize_hex_color( $colors&#91;'color'&#93; ?? '' );\n\t\tif ( '' === $id ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t$item = \"#adminmenu #{$id}\";\n\t\tif ( $bg || $color ) {\n\t\t\t$css .= \"{$item} .wp-menu-name{\" . ( $bg ? \"background:{$bg};\" : '' ) . ( $color ? \"color:{$color};\" : '' ) . '}';\n\t\t}\n\t\tif ( $color ) {\n\t\t\t$css .= \"{$item} .wp-menu-image:before{color:{$color};}\";\n\t\t}\n\t}\n\n\treturn $css;\n}\n\nadd_action( 'admin_enqueue_scripts', 'dpc_admin_menu_colors_enqueue' );\n\nfunction dpc_admin_menu_colors_enqueue() {\n\t$css = dpc_admin_menu_colors_css();\n\tif ( '' !== $css ) {\n\t\twp_add_inline_style( 'admin-menu', $css );\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>Each key in the config is the <code>id<\/code> of a menu item&#8217;s <code>&lt;li&gt;<\/code> in the admin sidebar. To find it, right-click the menu item, choose <strong>Inspect<\/strong> and look for the <code>&lt;li id=\"...\"&gt;<\/code> around the link. Posts is <code>menu-posts<\/code>, Media <code>menu-media<\/code>, a plugin page often <code>toplevel_page_<\/code> plus its slug.<\/li>\n\n\n<li>The CSS sets the background of the menu label and the colour of the label and its icon. The selectors start with <code>#adminmenu<\/code> and the item ID, so they beat the hover and current-item colours of every admin colour scheme without <code>!important<\/code>.<\/li>\n\n\n<li>The rules are attached to the <code>admin-menu<\/code> stylesheet with <code>wp_add_inline_style()<\/code> on <code>admin_enqueue_scripts<\/code>, so they load on every admin screen right after the menu CSS.<\/li>\n\n\n<li>IDs go through <code>sanitize_html_class()<\/code> and colours through <code>sanitize_hex_color()<\/code>, so a typo cannot break the stylesheet; invalid colours are skipped.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post printed a <code>&lt;style&gt;<\/code> tag with <code>!important<\/code> rules for Easy Digital Downloads and ACF menus only.<\/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>Admin only<\/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 menu only exists in wp-admin, so Admin only is all it needs.<\/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>Your own items: add a line such as <code>'toplevel_page_woocommerce' =&gt; array( 'background' =&gt; '#7f54b3', 'color' =&gt; '#ffffff' ),<\/code>. The commented lines show the IDs for Easy Digital Downloads and ACF.<\/li>\n\n\n<li>Only change the text: leave out <code>background<\/code>. Only the background: leave out <code>color<\/code>.<\/li>\n\n\n<li>Restyle the whole admin instead: see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-admin-dark-theme\/\">WordPress Admin Dark Theme<\/a>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use full hex colours such as <code>#fff<\/code> or <code>#00c69e<\/code>; names like <code>red<\/code> are skipped.<\/li>\n\n\n<li>Every user sees the colours, whatever admin colour scheme they picked in their profile.<\/li>\n\n\n<li>Plugins that rename their menu slug in an update change the <code>&lt;li&gt;<\/code> ID too; check the ID again if a colour disappears.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Color chosen WordPress admin menu items with your own background and text colours, so the screens you use daily stand out in the sidebar.<\/p>\n","protected":false},"author":1,"featured_media":46783,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Color WordPress Admin Menu Items - Code DPlugins","description":"Color chosen WordPress admin menu items with your own background and text colours, so the screens you use daily stand out in the sidebar."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[6],"dpc_library":[204],"class_list":["post-9951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-wp-admin"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/9951","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=9951"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/9951\/revisions"}],"predecessor-version":[{"id":46782,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/9951\/revisions\/46782"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46783"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=9951"}],"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=9951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=9951"},{"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=9951"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}