5

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.

1
  • 3
    You're so close - the handle of the stylesheet is just yasrcss, not yasrcss-css (WordPress adds the -css suffix for the id atttribute) Aug 14, 2018 at 9:34

2 Answers 2

5

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' );
}
1

To remove stylesheet from few selected pages, open your function.php file and put below code inside this file. In the page id array, it targets only specific page where you don't want to apply your CSS

add_action('init','_remove_style');
function _remove_style(){
   global $post;
   $pageID = array('20','30', '420');//Mention the page id where you do not wish to include that script

   if(in_array($post->ID, $pageID)) {
       wp_dequeue_style('your_style_sheet.css'); 
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.