0

Several years ago, not really having a handle on the best practice for running a MultiSite network, I often network activated plugins. Later I realized this was a mistake because not all the sites used some of the plugins. So I have unnecessary scripts and stylesheets on several sites.

What I've started doing for those unwanted plugin resources is wp_dequeue_style( 'style-sheet' ); and wp_deregister_style( 'style-sheet' ); in child-theme functions.php

My problem is that it doesn't work for all plugins. For example, https://wordpress.org/plugins/advanced-recent-posts/ is not following my function. I thought perhaps I would try to remove_action(); but I am not familiar with how this exactly works.

Here's what I'm working with in my functions.php and it works for other style-sheets.

function remove_unwanted_stylesheets() {

wp_dequeue_style( 'wp-advanced-rp-css' );
wp_deregister_style( 'wp-advanced-rp-css' );

}
add_action( 'wp_enqueue_scripts', 'remove_unwanted_stylesheets', 999 );

The plugin enqueue and add action is as follows.

function add_advanced_recent_posts_widget_stylesheet() {

    $plugin_dir = 'advanced-recent-posts-widget';

    if ( @file_exists( STYLESHEETPATH . '/advanced-recent-posts-widget.css' ) )

        $mycss_file = get_stylesheet_directory_uri() . '/advanced-recent-posts-widget.css';

    elseif ( @file_exists( TEMPLATEPATH . '/advanced-recent-posts-widget.css' ) )

        $mycss_file = get_template_directory_uri() . '/advanced-recent-posts-widget.css';

    else

        $mycss_file = plugins_url( 'css/advanced-recent-posts-widget.css',dirname(__FILE__) );





        wp_register_style( 'wp-advanced-rp-css', $mycss_file );

        wp_enqueue_style( 'wp-advanced-rp-css' );



}

add_action('wp_print_styles', 'add_advanced_recent_posts_widget_stylesheet');

Any insight into how I can effectively dequeue and unregister the stylesheet for this plugin is greatly appreciated.

0

Your issue is timing. If you check out this answer for detailed order you'll see that wp_print_styles occurs after wp_enqueue_scripts. So the earliest your dequeue would work in this case is wp_print_styles at 11 priority.

Really it's unconventional of that plugin's author to register at that point. wp_enqueue_scripts is intended hooks for that exactly so that others don't have troubles like this.

  • Ah, I figured it was something like this. You got me thinking and I ended up finding that this worked perfectly, remove_action('wp_print_styles', 'remove_advanced_recent_posts_widget_stylesheet'); – Jarmerson Mar 29 '16 at 15:48
0

Going off of the plugins source code I simply copied then changed the add_action(); to remove_action(); in my child-themes function.php

add_action('wp_print_styles', 'add_advanced_recent_posts_widget_stylesheet');

Replace add with remove.

remove_action('wp_print_styles','add_advanced_recent_posts_widget_stylesheet');

So simple. Now I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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