I’m not a WordPress developer, I’m trying to help a friend who has a WordPress website. Here is the problem:

In the template, wp_head() function adds bunch of styles and scripts.

I want to remove some of them in some pages lets say this one:

<link rel="stylesheet" id='yasrcss-css'  href="https://www.example.com/wp-content/plugins/yet-another-stars-rating/css/yasr.css" type="text/css" media="all" />

So I searched around and based on what I’ve been suggested on other questions, I added this lines to functions.php (inside theme folder):

add_action( 'init', '_remove_style' );

function _remove_style() {
    wp_dequeue_style( 'yasrcss-css' );
    wp_dequeue_style( 'yasr.css' );
}

It didnt work, also I added this in functions.php:

wp_deregister_style('yasrcss-css');

This one didn’t work either.

Am I missing something, is there anything else I should do ?

BTW, I tried this code:

printf( 
    '<pre>%s</pre>', 
    var_export( $GLOBALS['wp_scripts']->registered, TRUE )
);

as suggested here. It didn’t have that particular CSS in the output.

2 s
2

Note: the file name is functions.php, not function.php (but that’s probably just a typo in the question).

To remove a script or style, you must remove it after it was added. If you try to remove it before it was added, or even print $GLOBALS['wp_scripts']->registered, nothing will happen, since it was not added yet.

So a way to remove them is to execute the _remove_style function as late as possible.

Also, you need to make sure you are using the correct handle that was used to enqueue the CSS file in the first place. In this case, the correct handle is: yasrcss (credit to @thedeadmedic).

Combining all these, you may try the following CODE:

add_action( 'wp_enqueue_scripts', '_remove_style', PHP_INT_MAX );
function _remove_style() {
    wp_dequeue_style( 'yasrcss' );
}

Leave a Reply

Your email address will not be published. Required fields are marked *