{"id":46913,"date":"2026-10-12T08:06:59","date_gmt":"2026-10-12T08:06:59","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46913"},"modified":"2026-10-12T08:06:59","modified_gmt":"2026-10-12T08:06:59","slug":"tailwind-v4-wordpress-block-theme-without-plugin","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/tailwind-v4-wordpress-block-theme-without-plugin\/","title":{"rendered":"Add Tailwind v4 to a WordPress Block Theme Without a Plugin"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To use Tailwind with a WordPress block theme without a plugin, you have to solve three problems. You need Tailwind v4 classes that use the colors, fonts and spacing already defined in <code>theme.json<\/code>, not a second palette. Those classes have to work on the front end and inside the editor iframe. And Tailwind has to find the classes in your block markup, which lives in <code>.html<\/code> templates, parts and patterns rather than in JSX.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet covers all three. A WP-CLI command turns your <code>theme.json<\/code> presets into a Tailwind v4 <code>@theme<\/code> file, which means <code>bg-primary<\/code> points at <code>var(--wp--preset--color--primary)<\/code>. Change a color in the Site Editor and every Tailwind class follows. One <code>enqueue_block_assets<\/code> callback loads the compiled CSS on the front end and in the editor canvas. The build itself is the official Tailwind CLI, so you don&#8217;t need a bundler.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put this in your block theme&#8217;s <code>functions.php<\/code> or in a small plugin. It expects the compiled file at <code>assets\/css\/tailwind.css<\/code> in the theme.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Tailwind v4 in a block theme, no plugin.\n *\/\n\nfunction dpc_tailwind_css_path(): string {\n\treturn get_theme_file_path( 'assets\/css\/tailwind.css' );\n}\n\n\/**\n * Front end and the editor iframe both fire enqueue_block_assets.\n *\/\nfunction dpc_enqueue_tailwind(): void {\n\t$path = dpc_tailwind_css_path();\n\tif ( ! file_exists( $path ) ) {\n\t\treturn;\n\t}\n\twp_enqueue_style(\n\t\t'dpc-tailwind',\n\t\tget_theme_file_uri( 'assets\/css\/tailwind.css' ),\n\t\tarray(),\n\t\t(string) filemtime( $path )\n\t);\n}\nadd_action( 'enqueue_block_assets', 'dpc_enqueue_tailwind' );\n\n\/**\n * Build a Tailwind v4 @theme block that points at theme.json presets.\n *\/\nfunction dpc_tailwind_theme_tokens(): string {\n\t$settings = wp_get_global_settings();\n\n\t\/\/ Tailwind namespace =&gt; &#91; theme.json section, key, WP preset name, class prefix &#93;.\n\t$groups = array(\n\t\t'color'   =&gt; array( 'color', 'palette', 'color', '' ),\n\t\t'font'    =&gt; array( 'typography', 'fontFamilies', 'font-family', '' ),\n\t\t'text'    =&gt; array( 'typography', 'fontSizes', 'font-size', '' ),\n\t\t'spacing' =&gt; array( 'spacing', 'spacingSizes', 'spacing', 'wp-' ),\n\t);\n\n\t$tokens = array();\n\tforeach ( $groups as $namespace =&gt; list( $section, $key, $preset, $prefix ) ) {\n\t\t$presets = $settings&#91; $section &#93;&#91; $key &#93; ?? array();\n\t\tforeach ( array( 'theme', 'custom' ) as $origin ) {\n\t\t\tforeach ( $presets&#91; $origin &#93; ?? array() as $item ) {\n\t\t\t\tif ( empty( $item&#91;'slug'&#93; ) || ! is_string( $item&#91;'slug'&#93; ) ) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t$slug = preg_replace( '\/&#91;^a-z0-9-&#93;\/', '', strtolower( _wp_to_kebab_case( $item&#91;'slug'&#93; ) ) );\n\t\t\t\tif ( '' === $slug ) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t$tokens&#91; \"--{$namespace}-{$prefix}{$slug}\" &#93; = \"var(--wp--preset--{$preset}--{$slug})\";\n\t\t\t}\n\t\t}\n\t}\n\n\t$css  = \"\/* Generated from theme.json by `wp dpc tailwind-tokens`. Do not edit. *\/\\n\";\n\t$css .= \"@theme inline {\\n\";\n\tforeach ( $tokens as $name =&gt; $value ) {\n\t\t$css .= \"\\t{$name}: {$value};\\n\";\n\t}\n\t$css .= \"}\\n\";\n\n\treturn $css;\n}\n\nif ( defined( 'WP_CLI' ) &amp;&amp; WP_CLI ) {\n\tWP_CLI::add_command(\n\t\t'dpc tailwind-tokens',\n\t\tstatic function (): void {\n\t\t\tWP_CLI::line( dpc_tailwind_theme_tokens() );\n\t\t}\n\t);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, create <code>src\/tailwind.css<\/code> in the theme. This is the Tailwind entry file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">\/* Theme variables + utilities. No preflight: theme.json owns base styles. *\/\n@import \"tailwindcss\/theme.css\" layer(theme);\n@import \"tailwindcss\/utilities.css\";\n@import \".\/wp-tokens.css\";\n\n\/* Block markup lives here, not in JS. Paths are relative to this file. *\/\n@source \"..\/templates\";\n@source \"..\/parts\";\n@source \"..\/patterns\";\n@source \"..\/functions.php\";\n\n\/* Classes typed into the editor are stored in the database. List them here. *\/\n@source inline(\"{hover:,}bg-primary text-contrast\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Build it from the theme folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">npm install -D tailwindcss @tailwindcss\/cli\n\n# Regenerate tokens whenever theme.json presets change\nwp dpc tailwind-tokens &gt; src\/wp-tokens.css\n\n# Production build\nnpx @tailwindcss\/cli -i src\/tailwind.css -o assets\/css\/tailwind.css --minify\n\n# While developing\nnpx @tailwindcss\/cli -i src\/tailwind.css -o assets\/css\/tailwind.css --watch<\/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>dpc_tailwind_theme_tokens()<\/code> reads the merged settings with <code>wp_get_global_settings()<\/code>, takes the theme and user (custom) presets, and writes one Tailwind variable per preset. The <code>color<\/code> namespace becomes <code>bg-*<\/code>, <code>text-*<\/code> and <code>border-*<\/code>, <code>font<\/code> becomes <code>font-*<\/code>, and <code>text<\/code> becomes the font-size utilities.<\/li>\n\n\n<li><code>@theme inline<\/code> makes Tailwind put the <code>var(--wp--preset--\u2026)<\/code> reference straight into each utility. WordPress already prints those variables in global styles, so a palette change in the Site Editor updates your Tailwind classes without a rebuild.<\/li>\n\n\n<li>Spacing tokens get a <code>wp-<\/code> prefix (<code>p-wp-50<\/code>, <code>gap-wp-30<\/code>). That keeps them apart from Tailwind&#8217;s own numeric scale, so <code>p-4<\/code> still means what Tailwind says it means.<\/li>\n\n\n<li><code>enqueue_block_assets<\/code> runs on the front end and inside the iframed editor canvas, so blocks look the same in both places. The <code>filemtime()<\/code> version busts caches after every build.<\/li>\n\n\n<li>The <code>@source<\/code> lines tell Tailwind to scan your templates, parts and patterns, and only classes found there end up in the CSS. That is the v4 replacement for the old <code>content<\/code> array and purge config.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Want only your theme palette and none of Tailwind&#8217;s default colors? Add <code>--color-*: initial;<\/code> as the first line inside a <code>@theme<\/code> block in <code>src\/tailwind.css<\/code>.<\/li>\n\n\n<li>Need Tailwind&#8217;s preflight reset for a theme with no <code>theme.json<\/code> styles? Swap the first two imports for <code>@import \"tailwindcss\";<\/code>. Expect it to fight core block styles.<\/li>\n\n\n<li>To pick up the <code>default<\/code> origin as well (core&#8217;s color palette and spacing scale), add <code>'default'<\/code> to the origins array.<\/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>Tailwind only sees files on disk. A class someone types into the <strong>Additional CSS class(es)<\/strong> field of a block in a post is stored in the database, so it gets purged unless you list it in <code>@source inline()<\/code>. For patterns and templates, keep the classes in the theme files.<\/li>\n\n\n<li>Utilities are imported without a cascade layer on purpose. WordPress global styles are not layered, and unlayered CSS always beats layered CSS, so layered utilities would silently lose. This is the usual cause of &#8220;tailwind v4 theme not working&#8221; in block themes.<\/li>\n\n\n<li>Run <code>wp dpc tailwind-tokens<\/code> again after adding or renaming a preset. Changing a preset&#8217;s value doesn&#8217;t need a rebuild, but adding or renaming one does.<\/li>\n\n\n<li>Keep <code>node_modules<\/code> out of the theme zip you ship. Only <code>assets\/css\/tailwind.css<\/code> is needed at runtime.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If you only want custom editor CSS without Tailwind, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/css-in-gutenberg-editor\/\">CSS in the Gutenberg editor<\/a>. To control which core block CSS still loads, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/remove-the-gutenberg-block-library-css-from-wordpress\/\">Remove Gutenberg Block Library CSS<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;d rather not run a build step, <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/winden\/\">Winden<\/a> compiles Tailwind on the server and works with Gutenberg.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Integrate Tailwind v4 with a WordPress block theme without a plugin: theme.json colors as Tailwind classes, scanned templates, styles in the editor.<\/p>\n","protected":false},"author":1,"featured_media":46914,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Add Tailwind v4 to a WordPress Block Theme Without a Plugin","description":"Integrate Tailwind v4 with a WordPress block theme without a plugin: theme.json colors as Tailwind classes, scanned templates, styles in the editor."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[78,3],"tags":[201,213,212,157,149],"dpc_library":[],"class_list":["post-46913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tailwind","category-wordpress","tag-block-theme","tag-editor-styles","tag-tailwind","tag-theme-json","tag-wp-cli"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46913","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=46913"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46913\/revisions"}],"predecessor-version":[{"id":46915,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46913\/revisions\/46915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46914"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46913"}],"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=46913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46913"},{"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=46913"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}