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 );
}
1 Answer
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