4

I am creating a child theme. I noticed my parent theme is adding some inline CSS in its functions.php:

wp_add_inline_style( 'persona-style-css', $custom_css );

Since I cannot change some values there, is it possible to dequeue it? I have tried to dequeue it using wp_dequeue_style ('persona-style-css') but it didn't really help.

Thanks in advance.

2
  • Hi, welcome to WPSE. How exactly did you use wp_dequeue_style and it didn't work? Did you use it inside the same hook that the parent is using to enqueue it with a higher priority?
    – Johansson
    Jun 8, 2017 at 5:47
  • 1
    Check this wordpress.stackexchange.com/a/231821/43098
    – bravokeyl
    Jun 8, 2017 at 6:08

2 Answers 2

2

If wp_add_inline_css is fired within an action you can use remove_action with the same parameters.

You also might use wp_enqueue_scripts action to dequeue any scripts or styles in a proper way. But, inline style are not included in the $wp_styles global, you can unset them with the action print_styles_array, you need to know the handle name to unset it.

Hope it gives you some hints to make it works.

1
  • print_styles_array lists only non-inline queue (same as $wp_styles) too.
    – Fanky
    Mar 31, 2021 at 18:11
1

Try,

function wp_force_remove_style(){

add_filter( 'print_styles_array', function($styles) {

    #DEBUG: Show all registered styles
    //print_r($styles);
    //die();

    #Set styles to remove
    $styles_to_remove = array('persona-style-css');

    if(is_array($styles) AND count($styles) > 0){

        foreach($styles AS $key => $code){

            if(in_array($code, $styles_to_remove)){

                unset($styles[$key]);

            }

        }

    }

    return $styles;

    }); 

}

add_action('wp_enqueue_scripts', 'wp_force_remove_style', 99);

I hope this helps!

1
  • This doesn't walk through styles added by wp_add_inline_css() as asked, only through styles added by wp_enqueue_script()
    – Fanky
    Mar 31, 2021 at 18:03

Your Answer

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

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