2

Bit of an unusual one. I have created a plugin that will only display content on a custom page template. For this reason, I have dequeued all the theme styles via $wp_styles and loaded in the plugin css for this page template only custom-template.php - this is to reduce style conflicts of different themes and performance as it's not needed.

I have added in widget support on the custom page template but the problem is that all the styles are dequeued and the widgets are not styled.

Is there a way to remove all the stylesheets for the theme but keep the widget/plugin stylesheets in? Please note that this plugin will be used on various different themes so we can't assume what the theme stylesheet "handle" will be called.

global $wp_styles;
if ( is_page_template( 'custom-template.php' ) ) {    
     // get all styles data
        global $wp_styles;

        // create an array of stylesheet "handles" to allow to remain
        $styles_to_keep = array("wp-admin", "admin-bar", "dashicons", "open-sans");

        // loop over all of the registered scripts
        foreach ($wp_styles->registered as $handle => $data)
        {
            // if we want to keep it, skip it
            if ( in_array($handle, $styles_to_keep) ) continue;

            // otherwise remove it
            wp_deregister_style($handle);
            wp_dequeue_style($handle);
        }
     //Now add the plugin stylesheet
     wp_enqueue_style( $this->custom_style, plugin_dir_url( __FILE__ ) . 'css/plugin-public.css', array(), $this->version, 'all', 9999 );
}
  • by dequeuing styles, you loose all the styles for header, menus and sidebar I don't think that's what you want..you can conditionally enqueue a .css stylesheet for the page builded by the plugin leaving the rest untouched – Andrea Somovigo Jul 29 '18 at 22:46
  • @AndreaSomovigo I understand, but for this case I am not using the head, menus, footer, sidebar etc. I only need to be able to load in 3rd party plugin styles and default widget styles. – scopeak Jul 29 '18 at 22:49
0

You would have to enqueue them at the same time you dequeue all the other items, assuming your widgets have separate stylesheets.

You can't selectively enqueue sub-portions of a stylesheet, and there is no method of identifying which stylesheets contain the widgets. Depending on the theme the widgets may not be in their own stylesheet, or even a single stylesheet

You're almost certainly going to need to manually find the widget CSS rules and copy them into a separate stylesheet.

So can you do this by hand to make a specific theme work? Yes!

Can you do it in a generic way that works for every theme? No

  • Thanks Tom. Okay here's another idea - would it be possible to remove only the active theme main style.css but keep all the active plugins styles enqueued? Maybe this way I can add the base widget styling in my plugin. – scopeak Jul 30 '18 at 6:07
  • Possibly, but it would require you to modify your check to identify those stylesheets that come from the theme. Even then, the widgets will have the HTML classes etc that the theme defined, which means they will always require some theme specific styling – Tom J Nowell Jul 30 '18 at 12:20
  • Okay. Maybe I'll just need to allow the theme styling through and include resets to ensure the design of the plugin isn't affected. Thanks! – scopeak Jul 30 '18 at 19:57

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.