You add md:grid-cols-3 or hover:bg-brand to a block’s Additional CSS class(es) field in WordPress, hit save, and on the front end nothing changes. Tailwind v4 is not broken. It builds CSS only for class names it finds in files it scans, and those classes live in the database (wp_posts), not in your theme folder. The same goes for classes in Custom HTML blocks, synced patterns and templates you edited in the Site Editor.
The snippet below adds a WP-CLI command that reads every post, page, synced pattern, template and template part, collects the class names used in them, and prints a Tailwind v4 @source inline() safelist. You save that output next to your input CSS, import it, and rebuild. After that, Tailwind classes added in the editor show up on the site.
The snippet
Put this in a small plugin, an mu-plugin, your theme’s functions.php, or a Scripts Organizer PHP block. It only adds a CLI command and does nothing on normal page loads.
<?php
/**
* WP-CLI: wp dpc tailwind-safelist
* Prints a Tailwind v4 @source inline() safelist built from classes stored in post content.
*/
function dpc_tw_post_types(): array {
return (array) apply_filters(
'dpc_tailwind_safelist_post_types',
array( 'post', 'page', 'wp_block', 'wp_template', 'wp_template_part' )
);
}
function dpc_tw_walk_blocks( array $blocks, array &$found ): void {
foreach ( $blocks as $block ) {
if ( ! empty( $block['attrs']['className'] ) && is_string( $block['attrs']['className'] ) ) {
$found[] = $block['attrs']['className'];
}
if ( ! empty( $block['innerBlocks'] ) ) {
dpc_tw_walk_blocks( $block['innerBlocks'], $found );
}
}
}
function dpc_tw_is_candidate( string $token ): bool {
if ( strlen( $token ) > 120 ) {
return false;
}
// Characters that would break the inline() string or trigger brace expansion.
if ( false !== strpbrk( $token, '"\\{}' ) ) {
return false;
}
// Core block classes (has-text-color, wp-block-group, alignwide...). Variants with ":" are kept.
if ( preg_match( '/^(?:(?:wp-|has-|is-)[^:]*|align(?:wide|full|left|right|center|none))$/', $token ) ) {
return false;
}
return (bool) apply_filters( 'dpc_tailwind_safelist_keep', true, $token );
}
function dpc_tw_classes_from_content( string $content ): array {
$found = array();
// class="..." in saved block markup and Custom HTML blocks.
if ( preg_match_all( '/(?<![\w-])class\s*=\s*(["\'])(.*?)\1/is', $content, $matches ) ) {
foreach ( $matches[2] as $value ) {
$found[] = html_entity_decode( $value, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
}
}
// className attributes, also on dynamic blocks that save no markup.
dpc_tw_walk_blocks( parse_blocks( $content ), $found );
$tokens = preg_split( '/\s+/', implode( ' ', $found ), -1, PREG_SPLIT_NO_EMPTY );
return array_values( array_filter( array_unique( $tokens ), 'dpc_tw_is_candidate' ) );
}
function dpc_tw_collect_classes(): array {
$classes = array();
$per_page = 200;
$page = 1;
do {
$ids = get_posts(
array(
'post_type' => dpc_tw_post_types(),
'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ),
'posts_per_page' => $per_page,
'paged' => $page,
'fields' => 'ids',
'orderby' => 'ID',
'order' => 'ASC',
'no_found_rows' => true,
'suppress_filters' => true,
)
);
foreach ( $ids as $id ) {
$content = (string) get_post_field( 'post_content', $id, 'raw' );
foreach ( dpc_tw_classes_from_content( $content ) as $class ) {
$classes[ $class ] = true;
}
}
$page++;
} while ( count( $ids ) === $per_page );
$classes = array_keys( $classes );
sort( $classes, SORT_STRING );
return $classes;
}
function dpc_tw_safelist_css(): string {
$classes = dpc_tw_collect_classes();
$out = '/* Generated by wp dpc tailwind-safelist on ' . gmdate( 'Y-m-d H:i' ) . " UTC. Do not edit. */\n";
if ( ! $classes ) {
return $out . "/* No classes found in post content. */\n";
}
foreach ( array_chunk( $classes, 40 ) as $chunk ) {
$out .= '@source inline("' . implode( ' ', $chunk ) . "\");\n";
}
return $out;
}
function dpc_tw_register_cli(): void {
static $done = false;
if ( $done || ! class_exists( 'WP_CLI' ) ) {
return;
}
$done = true;
WP_CLI::add_command(
'dpc tailwind-safelist',
static function (): void {
foreach ( explode( "\n", rtrim( dpc_tw_safelist_css() ) ) as $line ) {
WP_CLI::line( $line );
}
},
array( 'shortdesc' => 'Print a Tailwind v4 @source inline() safelist from classes used in post content.' )
);
}
add_action( 'cli_init', 'dpc_tw_register_cli' );
// Loaded after cli_init already fired (e.g. late include)? Register right away.
if ( defined( 'WP_CLI' ) && WP_CLI && did_action( 'cli_init' ) ) {
dpc_tw_register_cli();
}
Generate the safelist into your theme’s source folder, then rebuild:
wp dpc tailwind-safelist > wp-content/themes/my-theme/src/safelist.css
cd wp-content/themes/my-theme
npx @tailwindcss/cli -i src/input.css -o build/style.css --minify
And import it in your Tailwind input file:
@import "tailwindcss";
@import "./safelist.css";
/* Your theme files are still scanned as usual. */
@source "../templates";
@source "../parts";
@source "../patterns";
If you haven’t wired Tailwind into a block theme yet, start with Add Tailwind v4 to a WordPress Block Theme Without a Plugin. To make your theme.json palette available as Tailwind utilities, see Tailwind v4 Design Tokens From WordPress theme.json.
How it works
- It pages through posts, pages, synced patterns (
wp_block), templates and template parts in batches of 200 IDs, so large sites don’t load everything at once. - Two sources per post:
class="..."attributes in the saved markup, and theclassNameattribute fromparse_blocks(). The second one matters for dynamic blocks like Query Loop or Latest Posts, which save no HTML. - Core classes such as
has-text-color,wp-block-groupandalignwideare dropped to keep the list short. Tailwind variants that look like them, such ashas-checked:bg-red-500, contain a:and are kept. - Tokens containing
",\,{or}are skipped, since they would break theinline("...")string or set off Tailwind’s brace expansion. - The output is split into one
@source inline()line per 40 classes, so diffs stay readable if you commit the file.
Customise it
- Scan a custom post type:
add_filter( 'dpc_tailwind_safelist_post_types', fn( $types ) => [ ...$types, 'product' ] ); - Drop more noise, for example classes from a slider plugin:
add_filter( 'dpc_tailwind_safelist_keep', fn( $keep, $c ) => $keep && ! str_starts_with( $c, 'swiper-' ), 10, 2 ); - Run the command in your deploy script just before the Tailwind build, so the safelist always matches production content.
Gotchas
@source inline()needs Tailwind v4.1 or newer. On 4.0, upgrade first.- This is a build-time step. A class an editor adds after the last build only shows up after you run the command and rebuild again.
- Extra entries are harmless. Tailwind ignores names that aren’t utilities, so a custom class like
hero-cardin the list just produces no CSS. - Classes you build in PHP from pieces (
'bg-' . $color) still won’t be found. Write the full class names somewhere Tailwind scans. - Want the same classes in the editor canvas? Load the built CSS with
add_editor_style()as well as on the front end.
Prefer not to run a build at all? Winden compiles Tailwind on the server and works with Gutenberg.
Snippet poll. Should this snippet be in the Scripts Organizer library for one-click import? 10 votes put it there.
0 of 10 votes