{"id":46940,"date":"2026-11-02T09:05:02","date_gmt":"2026-11-02T09:05:02","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46940"},"modified":"2026-11-02T09:05:02","modified_gmt":"2026-11-02T09:05:02","slug":"wordpress-theme-json-button-hover-focus-active","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-theme-json-button-hover-focus-active\/","title":{"rendered":"WordPress theme.json Button Hover, Focus and Active Styles"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Styling a WordPress theme.json button hover state is a common search, and the theme.json reference doesn&#8217;t make it obvious. The Site Editor lets you style buttons, but most themes give you little control over what happens when a visitor hovers over a button, tabs to it with the keyboard, or presses it. If the active theme is a parent theme like Twenty Twenty-Five, editing its <code>theme.json<\/code> isn&#8217;t an option either, because the next theme update overwrites your changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The snippet below adds <code>:hover<\/code>, <code>:focus-visible<\/code> and <code>:active<\/code> styles to the theme&#8217;s button element from PHP. They go into the same theme.json data that WordPress turns into global styles. This covers the Button block, the Search block button, the File block download button and any other block that outputs <code>.wp-element-button<\/code>, on the front end and in the editor. Put it in a small plugin, a child theme&#8217;s <code>functions.php<\/code>, or a Scripts Organizer PHP block.<\/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 * Plugin Name: DPC Button States\n * Description: Adds hover, focus-visible and active styles to the theme.json button element.\n *\/\n\n\/**\n * Button state styles in theme.json format.\n * Each colour uses a theme preset with a hard fallback, so it works on any theme.\n *\/\nfunction dpc_button_state_styles(): array {\n\t$states = array(\n\t\t':hover'         =&gt; array(\n\t\t\t'color' =&gt; array(\n\t\t\t\t'background' =&gt; 'var(--wp--preset--color--contrast, #111111)',\n\t\t\t\t'text'       =&gt; 'var(--wp--preset--color--base, #ffffff)',\n\t\t\t),\n\t\t),\n\t\t':focus-visible' =&gt; array(\n\t\t\t'outline' =&gt; array(\n\t\t\t\t'color'  =&gt; 'var(--wp--preset--color--contrast, #111111)',\n\t\t\t\t'offset' =&gt; '3px',\n\t\t\t\t'style'  =&gt; 'solid',\n\t\t\t\t'width'  =&gt; '2px',\n\t\t\t),\n\t\t),\n\t\t':active'        =&gt; array(\n\t\t\t'color' =&gt; array(\n\t\t\t\t'background' =&gt; 'color-mix(in srgb, var(--wp--preset--color--contrast, #111111) 75%, #ffffff)',\n\t\t\t\t'text'       =&gt; 'var(--wp--preset--color--base, #ffffff)',\n\t\t\t),\n\t\t),\n\t);\n\n\t$states  = (array) apply_filters( 'dpc_button_state_styles', $states );\n\t$allowed = array( ':hover', ':focus', ':focus-visible', ':active' );\n\n\treturn array_intersect_key( $states, array_flip( $allowed ) );\n}\n\n\/**\n * Merge the states into the theme origin of theme.json.\n *\/\nadd_filter( 'wp_theme_json_data_theme', 'dpc_add_button_states' );\nfunction dpc_add_button_states( $theme_json ) {\n\tif ( ! is_object( $theme_json ) || ! method_exists( $theme_json, 'update_with' ) ) {\n\t\treturn $theme_json;\n\t}\n\n\t$states = dpc_button_state_styles();\n\tif ( empty( $states ) ) {\n\t\treturn $theme_json;\n\t}\n\n\treturn $theme_json-&gt;update_with(\n\t\tarray(\n\t\t\t'version' =&gt; WP_Theme_JSON::LATEST_SCHEMA,\n\t\t\t'styles'  =&gt; array(\n\t\t\t\t'elements' =&gt; array(\n\t\t\t\t\t'button' =&gt; $states,\n\t\t\t\t),\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>wp_theme_json_data_theme<\/code> runs when WordPress reads the active theme&#8217;s <code>theme.json<\/code>. <code>update_with()<\/code> merges your array into the existing data, so the theme&#8217;s own button padding, radius and typography stay as they are and only the keys you set change.<\/li>\n\n\n<li>In theme.json, pseudo-classes are keys under <code>styles.elements.button<\/code> (and <code>styles.elements.link<\/code>). WordPress generates <code>.wp-element-button:hover, .wp-block-button__link:hover<\/code> rules from them, wrapped in <code>:where()<\/code> so they keep low specificity.<\/li>\n\n\n<li>Colours use <code>var(--wp--preset--color--contrast, #111111)<\/code>: if the theme has a <code>contrast<\/code> palette slug you get its colour, otherwise you get the fallback. Swap in your theme&#8217;s slugs.<\/li>\n\n\n<li>The allow-list throws away anything that isn&#8217;t a pseudo-class theme.json supports for buttons. Without it, WordPress would quietly drop the key and you&#8217;d be left wondering why nothing happened.<\/li>\n\n\n<li><code>:focus-visible<\/code> adds the outline only for keyboard focus, not for mouse clicks, so keyboard users get a clear focus ring and mouse users don&#8217;t see one after clicking.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Change the states from anywhere else in your code through the <code>dpc_button_state_styles<\/code> filter, for example to add a border colour on hover:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_filter( 'dpc_button_state_styles', function ( array $states ): array {\n\t$states&#91;':hover'&#93;&#91;'border'&#93; = array( 'color' =&gt; 'var(--wp--preset--color--contrast, #111111)' );\n\treturn $states;\n} );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you maintain the theme yourself, skip the PHP and write the same thing directly in <code>theme.json<\/code>. There you can use the shorter <code>var:preset|color|slug<\/code> syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n  \"$schema\": \"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/schemas.wp.org\/trunk\/theme.json\",\n  \"version\": 3,\n  \"styles\": {\n    \"elements\": {\n      \"button\": {\n        \":hover\": {\n          \"color\": {\n            \"background\": \"var:preset|color|contrast\",\n            \"text\": \"var:preset|color|base\"\n          }\n        },\n        \":focus-visible\": {\n          \"outline\": {\n            \"color\": \"var:preset|color|contrast\",\n            \"offset\": \"3px\",\n            \"style\": \"solid\",\n            \"width\": \"2px\"\n          }\n        }\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For hover styles on a custom button design rather than on every button, register a block style for <code>core\/button<\/code> instead. The approach is the same as in <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/gutenberg-block-styles-wordpress-headings\/\">Build Gutenberg Block Styles for WordPress Headings<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Colour picked in the block sidebar wins.<\/strong> If a button has a palette background chosen in its settings, WordPress adds a <code>has-*-background-color<\/code> class with <code>!important<\/code>, so the hover background can&#8217;t override it. Leave the colour at default and let theme.json handle it.<\/li>\n\n\n<li><strong>The Outline style ignores the hover background.<\/strong> Core&#8217;s <code>.is-style-outline<\/code> rule is more specific than the <code>:where()<\/code> rules from global styles. Hover text and outline colours still apply.<\/li>\n\n\n<li><strong>User styles override theme styles.<\/strong> Button colours saved in the Site Editor&#8217;s Styles panel (the user origin) are applied after the theme origin. If nothing changes, check Styles &gt; Blocks &gt; Button and reset it.<\/li>\n\n\n<li>Only button and link elements support pseudo-classes in theme.json. To style the navigation block, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/navigation-overlay-menu-background-color\/\">Change the Navigation Overlay Menu Background in WordPress<\/a>.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Add hover, focus-visible and active states to every WordPress block theme button through theme.json data, without editing the theme&#8217;s files.<\/p>\n","protected":false},"author":1,"featured_media":46941,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress theme.json Button Hover, Focus and Active Styles","description":"Add hover, focus-visible and active states to every WordPress block theme button through theme.json data, without editing the theme's files."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[133,3],"tags":[234,140,30,233,157],"dpc_library":[],"class_list":["post-46940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-themes","category-wordpress","tag-accessibility","tag-block-themes","tag-buttons","tag-global-styles","tag-theme-json"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46940","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=46940"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46940\/revisions"}],"predecessor-version":[{"id":46942,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46940\/revisions\/46942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46941"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46940"}],"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=46940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46940"},{"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=46940"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}