wp_enqueue_style( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, string $media = 'all' )
- Since
- 2.6.0
- Source
wp-includes/functions.wp-styles.php:173
Queues a CSS file for output in the page head, registering it under the given handle first when a source URL is supplied. It only works reliably when called on the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hook, since the underlying WP_Styles object may not exist yet earlier in the request. If a handle is already registered, passing a new $src on a later call does not replace the existing source; use wp_deregister_style first if you need to change it.
Description
Registers the style if source provided (does NOT overwrite) and enqueues.
Compatibility
- WordPress
- since 2.6.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. Should be unique.
$srcstringoptional- Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
Default empty.Default:'' $depsstring[]optional- An array of registered stylesheet handles this stylesheet depends on. Default empty array.Default:
array() $verstring|bool|nulloptional- String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version.
If set to null, no version is added.Default:false $mediastringoptional- The media for which this stylesheet has been defined.
Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.Default:'all'
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.
Enqueue a plugin stylesheet on the front end and confirm it queued
A plugin registers and enqueues its own stylesheet on the standard front end hook, then checks the queue in the footer.
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style(
'my-plugin-style',
'https://example.com/wp-content/plugins/my-plugin/style.css',
array(),
'1.0.0',
'all'
);
} );
add_action( 'wp_footer', function() {
if ( wp_style_is( 'my-plugin-style', 'enqueued' ) ) {
echo esc_html( 'my-plugin-style is in the enqueued styles queue.' );
} else {
echo esc_html( 'my-plugin-style was not enqueued on this page.' );
}
} );wp_style_is() with the 'enqueued' status is a handy way to confirm a style actually made it into the queue.
Check whether re-enqueuing a handle with a new $src replaces the old source
The style is enqueued twice under the same handle with different source URLs and version numbers to see which one wins.
$handle = 'demo-style';
wp_enqueue_style( $handle, 'https://example.com/css/first.css', array(), '1.0' );
wp_enqueue_style( $handle, 'https://example.com/css/second.css', array(), '2.0' );
$styles = wp_styles();
printf(
'Registered src for %s: %s (version %s)',
esc_html( $handle ),
esc_html( $styles->registered[ $handle ]->src ),
esc_html( $styles->registered[ $handle ]->ver )
);Calling wp_enqueue_style() outside of wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts can trigger a _doing_it_wrong notice; this snippet is for illustrating the no-overwrite behavior, not for production placement.
Common problems and fixes · 3
- Why isn't my stylesheet showing up in the page source at all?
- Why does passing a different $src on a second call not change the stylesheet URL?
- Why did the version query string on my stylesheet URL change or disappear?
Why isn't my stylesheet showing up in the page source at all?
Why does passing a different $src on a second call not change the stylesheet URL?
Why did the version query string on my stylesheet URL change or disappear?
Alternatives and related functions
wp_register_style- When you want to register a stylesheet's source and dependencies without immediately adding it to the output queue.
wp_deregister_style- When you need to remove or replace an already-registered handle before enqueuing it again with a different source.
wp_style_is- When you need to check whether a handle is registered, enqueued, or already printed rather than blindly calling wp_enqueue_style() again.
wp_add_inline_style- When you need to attach a small block of raw CSS to an already registered or enqueued stylesheet handle instead of loading another file.
Performance profile
How much work a call to wp_enqueue_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
- 17–31
- Plugin surface
- None
- Called by
- 50
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 31.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
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_enqueue_style() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 17 | _wp_scripts_maybe_doing_it_wrong(), wp_styles(), ->enqueue() |
| always | 31 | _wp_scripts_maybe_doing_it_wrong(), wp_styles(), explode(), ->add(), ->enqueue() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 31 instructions, 17–31 executed per call, 1 branch. 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.
Uses · 2
- _wp_scripts_maybe_doing_it_wrong()Helper function to output a _doing_it_wrong message when applicable.
- wp_styles()Initializes $wp_styles if it has not been set.
Used by · 50
- Custom_Background::admin_load()Sets up the enqueue for the CSS & JavaScript files.
- Custom_Image_Header::css_includes()Sets up the enqueue for the CSS files.
- Twenty_Fourteen_Ephemera_Widget::enqueue_scripts()Enqueues scripts.
- Twenty_Twenty_One_Custom_Colors::editor_custom_color_variables()Editor custom color variables.
- Twenty_Twenty_One_Dark_Mode::enqueue_scripts()Enqueues scripts and styles.
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Block::render()Generates the render output for the block.
- WP_Customize_Color_Control::enqueue()Enqueue scripts/styles for the color picker.
- WP_Customize_Manager::customize_preview_init()Prints JavaScript settings.
- WP_Customize_Nav_Menus::enqueue_scripts()Enqueues scripts and styles for Customizer pane.
- WP_Customize_Widgets::enqueue_scripts()Enqueues scripts and styles for Customizer panel and export data to JavaScript.
- WP_Duotone::output_footer_assets()Outputs all necessary SVG for duotone filters, CSS for classic themes.
Show all 50
- WP_Interactivity_API::data_wp_router_region_processor()Processes the `data-wp-router-region` directive.
- WP_Internal_Pointers::enqueue_scripts()Initializes the new feature pointers.
- WP_Widget_Media_Audio::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Audio::enqueue_preview_scripts()Enqueue preview scripts.
- WP_Widget_Media_Video::enqueue_preview_scripts()Enqueue preview scripts.
- _WP_Editors::enqueue_default_editor()Enqueue all editor scripts.
- _WP_Editors::enqueue_scripts()
- _wp_get_iframed_editor_assets()Collect the block editor assets that need to be loaded into the editor's iframe.
- _wp_theme_json_webfonts_handler()Runs the theme.json webfonts handler.
- add_thickbox()Enqueues the default ThickBox js and css.
- enqueue_block_styles_assets()Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
- iframe_header()Generic Iframe header for use with Thickbox.
- login_header()Outputs the login page header.
- register_and_do_post_meta_boxes()Registers the default post meta boxes, and runs the `do_meta_boxes` actions.
- twenty_twenty_one_scripts()Enqueues scripts and styles.
- twentyeleven_admin_enqueue_scripts()Enqueues styles and scripts for the theme options page.
- twentyeleven_block_editor_styles()Enqueues styles for the block-based editor.
- twentyeleven_enqueue_color_scheme()Enqueues the styles for the current color scheme.
- twentyeleven_scripts_styles()Enqueues scripts and styles for front end.
- twentyfifteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentyfifteen_scripts()Enqueues scripts and styles.
- twentyfourteen_admin_fonts()Enqueues font stylesheet to admin screen for custom header display.
- twentyfourteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentyfourteen_scripts()Enqueues scripts and styles for the front end.
- twentynineteen_editor_customizer_styles()Enqueues supplemental block editor styles.
- twentynineteen_scripts()Enqueues scripts and styles.
- twentyseventeen_block_editor_styles()Enqueues styles for the block-based editor.
- twentyseventeen_scripts()Enqueues scripts and styles.
- twentysixteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentysixteen_scripts()Enqueues scripts and styles.
- twentyten_block_editor_styles()Enqueues styles for the block-based editor.
- twentyten_scripts_styles()Enqueues scripts and styles for front end.
- twentythirteen_block_editor_styles()Enqueues styles for the block-based editor.
- twentythirteen_custom_header_fonts()Loads our special font CSS files.
- twentythirteen_scripts_styles()Enqueues scripts and styles for the front end.
- twentytwelve_block_editor_styles()Enqueues styles for the block-based editor.
- twentytwelve_custom_header_fonts()Loads our special font CSS file.
- twentytwelve_scripts_styles()Enqueues scripts and styles for front end.
Source code
function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); $wp_styles = wp_styles(); if ( $src ) { $_handle = explode( '?', $handle ); $wp_styles->add( $_handle[0], $src, $deps, $ver, $media ); } $wp_styles->enqueue( $handle );}Changelog
Introduced in 2.6.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 7.0.4 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.