Skip to content

Instantly share code, notes, and snippets.

@svandragt
Last active December 9, 2025 09:09
Show Gist options
  • Select an option

  • Save svandragt/9354de336a0683dc672b30d816bbcb2b to your computer and use it in GitHub Desktop.

Select an option

Save svandragt/9354de336a0683dc672b30d816bbcb2b to your computer and use it in GitHub Desktop.
Enqueue a theme style and opt into WP's small-CSS inlining.
<?php
/**
* Enqueue a theme style and opt into WP's small-CSS inlining.
*
* If the file is small enough (per core’s internal threshold), WP will inline it.
* Otherwise it remains a normal <link>.
*
* @param string $handle Unique style handle.
* @param string $relative_path Path relative to the theme root (e.g. 'assets/css/theme.css').
* @param array $deps Optional. Style dependencies.
* @param string|bool $ver Optional. Version string. Null/empty uses theme version. False omits version.
* @param string $media Optional. Media attribute. Default 'all'.
*/
function enqueue_smart_style(
string $handle,
string $relative_path,
array $deps = [],
$ver = null,
string $media = 'all'
) {
$abs = get_theme_file_path( $relative_path );
if ( ! $abs || ! file_exists( $abs ) ) {
return;
}
$uri = get_theme_file_uri( $relative_path );
// Versioning: default to theme version unless explicitly disabled with false.
if ( $ver === null || $ver === '' ) {
$ver = wp_get_theme()->get( 'Version' );
} elseif ( $ver === false ) {
$ver = null; // omit ?ver=
}
// Register as usual.
wp_register_style( $handle, $uri, $deps, $ver, $media );
// Opt in to core's small-CSS inlining: must be set before enqueue.
// Core will read the file at this absolute path and inline if it's small.
wp_style_add_data( $handle, 'path', $abs );
wp_enqueue_style( $handle );
}
function enqueue_scripts() {
enqueue_smart_style( 'my-theme','style.css' );
}
add_action( "wp_enqueue_scripts", "enqueue_scripts", 100 );
@svandragt
Copy link
Author

svandragt commented Dec 9, 2025

CSS inlining limit was doubled from 20k to 40k in WP 6.9, https://core.trac.wordpress.org/ticket/63018 Thanks Adam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment