0

Right I have a plugin I am making for a customer, this plugin is actually to display things on a window TV display they have which will connect to a webpage not linked publicly on the website.

I am looking to enqueue only the styles and scripts which that plugin has, and this is a major problem it seems because I know how to selectively check "is page" and then only do it for certain pages but this site has a lot ... and I mean A LOT of plugins which all enqueue styles & scripts as well as the template.

I can't be going into every plugin some custom some public to add conditional statements to each enqueue to check if the page is not the TV display.

So how could I get around this issue? There does not seem to be a hook or filter that would help me here, any ideas?

1 Answer 1

0

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.

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.