wp_style_is()WP 2.8.0

Determines whether the stylesheet has been registered / enqueued for output / output to the screen.

wp_script_is() — The same check for a script file.

No Hooks.

Returns

true|false.

Usage

wp_style_is( $handle, $list );
$handle(string) (required)
The name of the stylesheet to check. A lowercase string.
$list(string)

Which condition to check. Can be:

  • registered — the stylesheet has been registered using wp_register_style().
  • enqueued or queue — styles have been enqueued.
  • done — styles have been output to the screen.
  • to_do — styles have not yet been output to the screen.

Default: 'enqueued'

Examples

1

#1 Demo

Suppose we added a styles file to the output queue, registering it beforehand:

// connect the styles
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

function theme_name_scripts() {
	wp_enqueue_style( 'style-name', get_stylesheet_uri() );
}

Now, let's do the same thing, only with a preliminary check whether the file is already added to the queue, if it is added do nothing:

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

function theme_name_scripts() {

	// styles are not connected, let's connect them
	if( ! wp_style_is( 'style-name' ) ){
		wp_enqueue_style( 'style-name', get_stylesheet_uri() );
	}
}

Changelog

Since 2.8.0 Introduced.

wp_style_is() code WP 7.0

function wp_style_is( $handle, $status = 'enqueued' ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	return (bool) wp_styles()->query( $handle, $status );
}