wppaste
WordPress

wp_add_inline_style( string $handle, string $data ): bool

Since
3.3.0
Source
wp-includes/functions.wp-styles.php:87

Appends a raw CSS string to a stylesheet handle that is already queued, so WordPress prints it inline next to that stylesheet's link tag. It only works if the handle has been passed to wp_enqueue_style() or wp_register_style() first; otherwise the call is silently ignored. Calling it more than once for the same handle stacks the CSS blocks in the order added, so later blocks can override earlier declarations.

Adds extra CSS styles to a registered stylesheet.

Description

Styles will only be added if the stylesheet is already in the queue.
Accepts a string $data containing the CSS. If two or more CSS code blocks are added to the same stylesheet $handle, they will be printed in the order they were added, i.e. the latter added styles can redeclare the previous.

Compatibility

WordPress
since 3.3.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.

Parameters

$handlestring
Name of the stylesheet to add the extra styles to.
$datastring
String containing the CSS styles to be added.

Return value

bool
True on success, false on failure.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Add inline CSS built from post meta to a style-only handle

Register a handle with no source file purely to hold inline CSS, then build that CSS from a post's meta value.

wp_enqueue_style( 'price-badge-style', false );

$price = get_post_meta( 2, 'price', true );

$css = sprintf(
	'.price-badge::after { content: "%s"; font-weight: bold; }',
	esc_attr( $price )
);

$added = wp_add_inline_style( 'price-badge-style', $css );

echo $added ? 'Inline style added.' : 'Inline style NOT added.';
echo "\n";
echo esc_html( wp_styles()->print_inline_style( 'price-badge-style', false ) );

Passing false as the src argument to wp_enqueue_style() is the usual pattern for a handle that only ever carries inline CSS.

Stack two inline CSS blocks on the same handle and see which one wins

Call wp_add_inline_style() twice for one handle to confirm the later block overrides the earlier one when printed.

wp_enqueue_style( 'accent-color-style', false );

wp_add_inline_style( 'accent-color-style', '.site-title { color: #2271b1; }' );
wp_add_inline_style( 'accent-color-style', '.site-title { color: #d63638; }' );

echo esc_html( wp_styles()->print_inline_style( 'accent-color-style', false ) );

Both blocks are kept and printed in the order they were added, not merged or deduplicated.

Common problems and fixes · 4

Why does wp_add_inline_style() return false and add nothing?

The stylesheet handle has to already be registered or enqueued before you attach inline CSS to it. Internally the call is delegated to WP_Styles::add_inline_style(), which has nothing to attach the CSS to if the handle isn't in the queue yet.

Why is my CSS getting cut down to just the middle of my style block?

If the $data you pass contains a literal </style> tag, the function detects it with stripos(), fires a _doing_it_wrong() notice, and strips the wrapper tags with a regex before saving the CSS.

Why am I getting a _doing_it_wrong notice about calling this too early?

The function opens with a call to _wp_scripts_maybe_doing_it_wrong(), which flags handles used before WordPress has finished setting up the style queue for the current request.

Does wp_add_inline_style() escape the CSS I pass in?

No, the source only checks for a stray </style> tag and otherwise stores $data as-is. Any values you interpolate into the CSS string, like post meta or user input, are your responsibility to sanitize.

Alternatives and related functions

wp_enqueue_style
When you need to load an actual CSS file from a URL rather than attach a snippet to an already-queued handle.
wp_register_style
When you want to register a handle now but delay enqueueing it, so you can still add inline CSS to it before it's printed.
wp_add_inline_script
When the extra code you need to output is JavaScript tied to a script handle instead of CSS tied to a style handle.
WP_Styles::add_inline_style
When you're already holding a reference to the global $wp_styles object and want to call the method directly instead of the wrapper function.

Performance profile

How much work a call to wp_add_inline_style() 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
19–36

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 36.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
35

35 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below wp_add_inline_style()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always19_wp_scripts_maybe_doing_it_wrong(), stripos(), wp_styles(), ->add_inline_style()
always36_wp_scripts_maybe_doing_it_wrong(), stripos(), __(), sprintf(), _doing_it_wrong(), wp_styles(), ->add_inline_style()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3619–361
8.53619–361
8.43619–3615 fewer instructions than PHP 8.3
8.34119–411
8.24119–411
8.14119–411
7.44119–411

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.

Uses · 6

Used by · 35

Show all 35

Source code

function wp_add_inline_style( $handle, $data ) {	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); 	if ( false !== stripos( $data, '</style>' ) ) {		_doing_it_wrong(			__FUNCTION__,			sprintf(				/* translators: 1: <style>, 2: wp_add_inline_style() */				__( 'Do not pass %1$s tags to %2$s.' ),				'<code>&lt;style&gt;</code>',				'<code>wp_add_inline_style()</code>'			),			'3.7.0'		);		$data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );	} 	return wp_styles()->add_inline_style( $handle, $data );}

Changelog

Introduced in 3.3.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/functions.wp-styles.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.