wppaste
WordPress

wp_maybe_inline_styles()

Since
5.8.0
Source
wp-includes/script-loader.php:3059
Allows small styles to be inlined.

Description

This improves performance and sustainability, and is opt-in. Stylesheets can opt in by adding path data using wp_style_add_data, and defining the file's absolute path:

wp_style_add_data( $style_handle, 'path', $file_path );

Compatibility

WordPress
since 5.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to wp_maybe_inline_styles() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7

Executed per call on PHP 8.5. The body compiles to 118.

Plugin surface
1 hook

Third-party callbacks on 'styles_inline_size_limit' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_maybe_inline_styles() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7none

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 118 instructions, 7 executed per call, 12 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_maybe_inline_styles() runs, in this order:

  1. apply_filters( styles_inline_size_limit )filterline 3071 (+12 into the body)

    The maximum size of inlined styles in bytes.

Uses · 3

Source code

function wp_maybe_inline_styles() {	global $wp_styles; 	$total_inline_limit = 40000;	/**	 * The maximum size of inlined styles in bytes.	 *	 * @since 5.8.0	 * @since 6.9.0 The default limit increased from 20K to 40K.	 *	 * @param int $total_inline_limit The file-size threshold, in bytes. Default 40000.	 */	$total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit ); 	$styles = array(); 	// Build an array of styles that have a path defined.	foreach ( $wp_styles->queue as $handle ) {		if ( ! isset( $wp_styles->registered[ $handle ] ) ) {			continue;		}		$src  = $wp_styles->registered[ $handle ]->src;		$path = $wp_styles->get_data( $handle, 'path' );		if ( $path && $src ) {			$size = wp_filesize( $path );			if ( ! $size ) {				continue;			}			$styles[] = array(				'handle' => $handle,				'src'    => $src,				'path'   => $path,				'size'   => $size,			);		}	} 	if ( ! empty( $styles ) ) {		// Reorder styles array based on size.		usort(			$styles,			static function ( $a, $b ) {				return ( $a['size'] <= $b['size'] ) ? -1 : 1;			}		); 		/*		 * The total inlined size.		 *		 * On each iteration of the loop, if a style gets added inline the value of this var increases		 * to reflect the total size of inlined styles.		 */		$total_inline_size = 0; 		// Loop styles.		foreach ( $styles as $style ) { 			// Size check. Since styles are ordered by size, we can break the loop.			if ( $total_inline_size + $style['size'] > $total_inline_limit ) {				break;			} 			// Get the styles if we don't already have them.			$style['css'] = file_get_contents( $style['path'] ); 			/*			 * Check if the style contains relative URLs that need to be modified.			 * URLs relative to the stylesheet's path should be converted to relative to the site's root.			 */			$style['css'] = _wp_normalize_relative_css_links( $style['css'], $style['src'] ); 			// Keep track of the original `src` for the style that was inlined so that the `sourceURL` comment can be added.			$wp_styles->add_data( $style['handle'], 'inlined_src', $style['src'] ); 			// Set `src` to `false` and add styles inline.			$wp_styles->registered[ $style['handle'] ]->src = false;			if ( empty( $wp_styles->registered[ $style['handle'] ]->extra['after'] ) ) {				$wp_styles->registered[ $style['handle'] ]->extra['after'] = array();			}			array_unshift( $wp_styles->registered[ $style['handle'] ]->extra['after'], $style['css'] );

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/script-loader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.