Dequeue theme stylesheets but keep widget styling on custom page template

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

Read more here:: Dequeue theme stylesheets but keep widget styling on custom page template

Leave a Reply

Your email address will not be published. Required fields are marked *