wppaste
WordPress

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.

Enqueues a CSS stylesheet.

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?

The function calls _wp_scripts_maybe_doing_it_wrong(), which flags calls made before the styles system is ready. Calling wp_enqueue_style() directly in a template file or plugin bootstrap, instead of inside a hook callback, means the stylesheet either never gets enqueued or triggers a notice.

Why does passing a different $src on a second call not change the stylesheet URL?

The source only calls $wp_styles->add() when $src is truthy, and WP_Dependencies::add() does not overwrite an existing registration for the same handle. Once a handle is registered, later calls to wp_enqueue_style() with that handle just enqueue it, ignoring any new $src, $deps, $ver, or $media you pass.

Why did the version query string on my stylesheet URL change or disappear?

The $ver parameter has three distinct meanings in the source's underlying logic: a string sets an explicit cache-busting version, false (the default) auto-fills the current WordPress version, and null suppresses the version query string entirely. Mixing these up is a common source of unexpected cache-busting behavior after a WordPress upgrade.

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
17–31

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

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.

WhenInstructionsCalls it makes
always17_wp_scripts_maybe_doing_it_wrong(), wp_styles(), ->enqueue()
always31_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

Used by · 50

Show all 50

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.

  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.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.