wp_dequeue_style()
Removes a CSS stylesheet from the output queue. A file is added to the queue using wp_enqueue_style().
Use wp_deregister_style() when you need to completely remove a stylesheet from the global registered styles data.
No Hooks.
Returns
null. Returns nothing.
Usage
wp_dequeue_style( $handle );
- $handle(string) (required)
- The identifier of the stylesheet to be removed from the output queue. The identifier is set when registering styles in the functions: wp_register_style() or wp_enqueue_style().
To easily find out the style identifier, read the question "Can't disable plugin styles".
Examples
#1 Removing (dequeue) a style file from the queue
To dequeue a style, it has to have been registered before you try to remove it. The best way to achieve this is to set a higher priority for your event and then run it.
Suppose we have a http://example.com/.../my_style.css style file enqueued to output in HEAD of the HTML code:
add_action( 'wp_enqueue_scripts', 'my_enqueue_style' );
function my_enqueue_style(){
wp_enqueue_style( 'my_style', get_template_directory_uri() . '/my_style.css' );
}
Now in our plugin, or child theme, we want to remove this stylesheet from being loaded.
This can be achieved by running wp_dequeue_style() after wp_enqueue_style() and using the same handler my_style. To be sure that we call 'dequeue' function after 'enqueue' let's use later priority for the hook function call. Default value of 10, so we just need a value of 11 to run later.
add_action( 'wp_enqueue_scripts', 'my_dequeue_style', 11 );
function my_dequeue_style(){
wp_dequeue_style( 'my_style' );
} #2 Style can't be dequeued if it is in dependencies list.
This function cannot dequeue the handle that is in dependencies list.
For example: I have enqueue this.
add_action( 'wp_enqueue_scripts', 'wp_kama_enqueue' );
function wp_kama_enqueue() {
wp_enqueue_style( 'my_style', plugin_dir_url(__FILE__) . 'assets/css/style.css', [ 'my_style_two' ] );
}
If we try to dequeue the my_style_two style, it will not work and it will be enqueue anyway.
add_action( 'wp_enqueue_scripts', 'wp_kama_dequeue', 100 );
function wp_kama_dequeue() {
wp_dequeue_style( 'my_style_two' );
}
Notes
Changelog
| Since 3.1.0 | Introduced. |
wp_dequeue_style() wp dequeue style code WP 6.9.1
function wp_dequeue_style( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
wp_styles()->dequeue( $handle );
}