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.
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?
- Why is my CSS getting cut down to just the middle of my style block?
- Why am I getting a _doing_it_wrong notice about calling this too early?
- Does wp_add_inline_style() escape the CSS I pass in?
Why does wp_add_inline_style() return false and add nothing?
Why is my CSS getting cut down to just the middle of my style block?
Why am I getting a _doing_it_wrong notice about calling this too early?
Does wp_add_inline_style() escape the CSS I pass in?
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
- Scaling
- Constant
- Instructions
- 19–36
- Plugin surface
- None
- Called by
- 33
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 36.
Nothing here hands control to plugin code.
33 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 19 | _wp_scripts_maybe_doing_it_wrong(), stripos(), wp_styles(), ->add_inline_style() |
| always | 36 | _wp_scripts_maybe_doing_it_wrong(), stripos(), __(), sprintf(), _doing_it_wrong(), wp_styles(), ->add_inline_style() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 36 | 19–36 | 1 | |
| 8.5 | 36 | 19–36 | 1 | |
| 8.4 | 36 | 19–36 | 1 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 41 | 19–41 | 1 | |
| 8.2 | 41 | 19–41 | 1 | |
| 8.1 | 41 | 19–41 | 1 | |
| 7.4 | 41 | 19–41 | 1 |
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
- _wp_scripts_maybe_doing_it_wrong()Helper function to output a _doing_it_wrong message when applicable.
- stripos()
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- wp_styles()Initializes $wp_styles if it has not been set.
- wp_styles()::add_inline_style()
Used by · 33
- Twenty_Twenty_One_Custom_Colors::custom_color_variables()Customizer & frontend custom color variables.
- Twenty_Twenty_One_Custom_Colors::editor_custom_color_variables()Editor custom color variables.
- Twenty_Twenty_One_Dark_Mode::editor_custom_color_variables()Enqueues editor custom color variables & scripts.
- WP_Duotone::output_footer_assets()Outputs all necessary SVG for duotone filters, CSS for classic themes.
- WP_Duotone::output_global_styles()Appends the used global style duotone filter presets (CSS custom properties) to the inline global styles CSS.
- WP_Interactivity_API::data_wp_router_region_processor()Processes the `data-wp-router-region` directive.
- _wp_theme_json_webfonts_handler()Runs the theme.json webfonts handler.
- enqueue_block_styles_assets()Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
- twenty_twenty_one_non_latin_languages()Enqueues non-latin language styles.
- twentyfifteen_color_scheme_css()Enqueues front-end CSS for color scheme.
- twentyfifteen_header_background_color_css()Enqueues front-end CSS for the header background color.
- twentyfifteen_post_nav_background()Add featured image as background image to post navigation elements.
Show all 33
- twentyfifteen_sidebar_text_color_css()Enqueues front-end CSS for the sidebar text color.
- twentynineteen_editor_customizer_styles()Enqueue supplemental block editor styles.
- twentysixteen_color_scheme_css()Enqueues front-end CSS for color scheme.
- twentysixteen_link_color_css()Enqueues front-end CSS for the link color.
- twentysixteen_main_text_color_css()Enqueues front-end CSS for the main text color.
- twentysixteen_page_background_color_css()Enqueues front-end CSS for the page background color.
- twentysixteen_secondary_text_color_css()Enqueues front-end CSS for the secondary text color.
- twentytwenty_block_editor_styles()Enqueue supplemental block editor styles.
- twentytwenty_non_latin_languages()Enqueue non-latin language styles.
- twentytwenty_register_styles()Register and Enqueue Styles.
- wp_add_global_styles_for_blocks()Adds global style rules to the inline style for each block.
- wp_enqueue_admin_bar_bump_styles()Enqueues inline bump styles to make room for the admin bar.
- wp_enqueue_admin_bar_header_styles()Enqueues inline style to hide the admin bar when printing.
- wp_enqueue_block_template_skip_link()Enqueues the skip-link script & styles.
- wp_enqueue_embed_styles()Enqueues the CSS in the embed iframe header.
- wp_enqueue_emoji_styles()Enqueues the important emoji-related styles.
- wp_enqueue_global_styles()Enqueues the global styles defined via theme.json.
- wp_enqueue_global_styles_css_custom_properties()Function that enqueues the CSS Custom Properties coming from theme.json.
- wp_enqueue_global_styles_custom_css()Enqueues the global styles custom css defined via theme.json.
- wp_enqueue_stored_styles()Fetches, processes and compiles stored core styles, then combines and renders them to the page.
- wp_render_block_style_variation_support_styles()Renders the block style variation's styles.
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><style></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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.