Last active
March 3, 2025 05:47
-
-
Save westonruter/09e553a7b66d1a2e68cd5a9ed351c59b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Twenty Twenty-Five Stylesheet Inlining | |
* Plugin URI: https://gist.github.com/westonruter/09e553a7b66d1a2e68cd5a9ed351c59b | |
* Description: Minifies and adds <code>path</code> style data for the <code>twentytwenty-five-style</code> stylesheet so it can be inlined. See <a href="https://core.trac.wordpress.org/ticket/63007">#63007</a>. | |
* Requires at least: 6.5 | |
* Requires PHP: 7.2 | |
* Version: 0.1.0 | |
* Author: Weston Ruter | |
* Author URI: https://weston.ruter.net/ | |
* License: GPLv2 or later | |
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* Update URI: https://gist.github.com/westonruter/09e553a7b66d1a2e68cd5a9ed351c59b | |
* GitHub Plugin URI: https://gist.github.com/westonruter/09e553a7b66d1a2e68cd5a9ed351c59b | |
* | |
* @package TwentyTwentyFiveStylesheetInlining | |
*/ | |
namespace TwentyTwentyFiveStylesheetInlining; | |
add_action( | |
'wp_enqueue_scripts', | |
static function () { | |
wp_style_add_data( | |
'twentytwentyfive-style', | |
'path', | |
get_parent_theme_file_path( 'style.css' ) | |
); | |
}, | |
20 | |
); | |
function get_theme_style_css(): string { | |
static $css = null; | |
if ( null !== $css ) { | |
return $css; | |
} | |
$css = file_get_contents( get_parent_theme_file_path( 'style.css' ) ); // 2,505 bytes before minification | |
if ( ! SCRIPT_DEBUG ) { | |
// Strip out CSS comments, collapse whitespace, and remove unneeded spaces in this specific CSS file. | |
// This works for TT5's theme CSS, but it is not a complete solution applicable to any stylesheet. | |
// For a general solution, a proper CSS tokenizer should be used. Alternatively, the CSS should go a build step so that there is a style.min.css minified version available! | |
$css = trim( preg_replace( '#/\*.*?\*/\s*#s', '', $css ) ); | |
$css = preg_replace( '/\s+/', ' ', $css ); | |
$css = preg_replace( '/(?<=[{};:,+]) | (?=[{};:,!+])/', '', $css ); | |
$css = str_replace( ';}', '}', $css ); | |
// 586 bytes after minification | |
} | |
return $css; | |
} | |
add_filter( | |
'pre_wp_filesize', | |
static function ( $size, $path ) { | |
if ( get_parent_theme_file_path( 'style.css' ) === $path ) { | |
$size = strlen( get_theme_style_css() ); | |
} | |
return $size; | |
}, | |
10, | |
2 | |
); | |
add_action( | |
'wp_head', | |
static function () { | |
$style = wp_styles()->query( 'twentytwentyfive-style' ); | |
if ( $style && $style->src === false ) { | |
$style->extra['after'][0] = get_theme_style_css(); | |
} | |
}, | |
2 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment