Support » Fixing WordPress » Can't dequeue a plugin css file

  • Resolved drazon

    (@drazon)


    Hi there is a plugin which adds a css file which I want to dequeue

    public function addStyles_frontend() {
    // Enqueue Styles
    wp_enqueue_style( 'prefix-font-awesome' , '//netdna.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' , array() , '4.3.0' );
    				}

    In my child theme fucntions.php I have this code

    function remove_unwanted_css(){
    	wp_dequeue_style('prefix-font-awesome');
    }
    add_action( 'wp_enqueue_scripts', 'remove_unwanted_css', 100 );

    But it doesn’t dequeue the css file…So what am I doing wrong?

    Thanks

Viewing 12 replies - 1 through 12 (of 12 total)
  • Try to add the following code

    function remove_unwanted_css(){
    	wp_dequeue_style('prefix-font-awesome');
    }
    add_action( 'wp_print_styles', 'remove_unwanted_css', 100 );

    Unfortunately this doesn’t help.
    A closer look at the plugin code reveals that the style is enqueued from a function which is called from the plugin shortcode.

    public function processShortcode( $p ) {
    				// enqueue our scripts + styles
    				$this->addScripts_frontend();
    				$this->addStyles_frontend();
    .....
    }

    So I guess what I have been trying so far is to dequeue a style that is not enqueued when running the child function.php and then the shortcode enqueues the style ignoring the code in functions.php which has already run. So what do I have to do? Replace the whole function processShortcode in functions.php?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Do you need to dequeue it? Can you not just override it with CSS specificity?

    Can you give me the link to that plugin so I can check it myself?

    Yes the link is this https://wordpress.org/plugins/timeline-express/ I have edited the plugin file directly which is /classes/class.timeline-express.php line 936. I guess that I will have to do it over and over again after every plugin update.

    I don’t want to override it with css as I already have an http request to font awesome from the theme which is a different version than the one which the plugin uses. So I wanted to avoid the extra request. In addition I wanted to experiment with this option to see how it is possible to dequeue css files in general from plugins

    Hello drazon,
    If you are taking about the style from the admin end then I hope these code will work for you.

    function remove_unwanted_css(){
    	wp_dequeue_style('prefix-font-awesome');
    }
    add_action( 'admin_enqueue_scripts', 'remove_unwanted_css', 100);

    Hi Tremi, no I’m talking about the front end style of font awesome at line 936

    Try these code:

    function remove_unwanted_css(){
    	wp_dequeue_style('prefix-font-awesome');
    }
    add_filter('wp_footer', 'remove_unwanted_css');

    Thank you very much this did the trick and the extra http request has been removed! Good technique

    So to understand wp_footer runs after the shortcode. The shortcode enqueues the css style but before the page is rendered wp_footer dequeues the css style, so the file is not requested from the browser… Is it something like that?

    Hello drazon,

    In the above code we have add a filter to the footer because the style is enqueued in the wp_footer. By filter the wp_footer it will check if the style handler “prefix-font-awesome” is present and dequeue it.

    For more information on wp_footer check this link

    Thank you very much Tremi for Your help!

    Anytime 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can't dequeue a plugin css file’ is closed to new replies.