Styles and Scripts, Selectively enqueue across entire site

Right I figured this out myself not sure how “hacky” this is but basically this is the code:

public function filter_scripts_styles(){
    if(is_page($this->plugin_name)){
        //Same name is used for the style as the js file so can reuse this array
        $allowed = array('jquery', $this->plugin_name.'_scrollpath', $this->plugin_name);
        //Check Scripts First
        global $wp_scripts;
        foreach($wp_scripts->queue as $handle ) {
            if(!in_array($handle, $allowed)){
                //echo 'Dequeuing : '. $handle;
                wp_dequeue_script($handle);
            }
        }
        //Check styles
        global $wp_styles;
        foreach($wp_styles->queue as $handle ) {
            if(!in_array($handle, $allowed)){
                //echo 'Dequeuing : '. $handle;
                wp_dequeue_style($handle);
            }
        }
    }
}

Basically I added an action to:

$this->loader->add_action( 'wp_print_styles', $plugin_public, 'filter_scripts_styles' );

Which if your using normal WP would be;

add_action('wp_print_styles', 'filter_scripts_styles');

I added the names of the handles of the things I knew were needed on that page dequeued everything else.

Hopefully someone else will find this useful.

Leave a Comment