How to dequeue / deregister any theme styles and scripts
Question
Is it possible to some how make my plugin dequeue / deregister any styles and any scripts from what ever theme activated. so it doesn't matter what theme will be installed the styles and scripts of that theme will be dequeue / deregister?
Just to be super clear:
- I don't know what theme will be used.
- I need to dequeue / deregister only the styles and scripts from the theme and not from other plugins.
Solution
The tricky thing is knowing whether or not a particular script or style was enqueued by the theme.
Themes and plugins both use the same hooks and functions, so they're not explicitly labelled in any way as belonging to a specific theme or plugin. This means that the only way to know whether a script or style is from the theme is to check the URL to see whether the the script/style's URL is pointing to somewhere in the theme directory.
One way you can do this is to loop over $wp_scripts->registered
and $wp_styles->registered
, and check the URL of each script and style against get_theme_root_uri()
which tells you the URL to the themes folder. If the script/style appears to be inside that folder, you can dequeue it:
function wpse_340767_dequeue_theme_assets() {
$wp_scripts = wp_scripts();
$wp_styles = wp_styles();
$themes_uri = get_theme_root_uri();
foreach ( $wp_scripts->registered as $wp_script ) {
if ( strpos( $wp_script->src, $themes_uri ) !== false ) {
wp_deregister_script( $wp_script->handle );
}
}
foreach ( $wp_styles->registered as $wp_style ) {
if ( strpos( $wp_style->src, $themes_uri ) !== false ) {
wp_deregister_style( $wp_style->handle );
}
}
}
add_action( 'wp_enqueue_scripts', 'wpse_340767_dequeue_theme_assets', 999 );
This will only work if the stylesheet or script is inside the theme. If the theme is enqueueing scripts or styles from a CDN, then I'm not sure if it's possible to target those.
OTHER TIPS
May be this will helps you. try
#For dequeue JavaScripts
function remove_unnecessary_scripts() {
# pass Name of the enqueued js.
# dequeue js
wp_dequeue_script( 'toaster-js' );
# deregister js
wp_deregister_script( 'toaster-js' );
}
add_action( 'wp_print_scripts', 'remove_unnecessary_scripts' );
#For dequeue Styles
function remove_unnecessary_styles() {
# pass Name of the enqueued stylesheet.
# dequeue style
wp_dequeue_style( 'custom-style' );
# deregister style
wp_deregister_style( 'custom-style' );
}
add_action( 'wp_print_styles', 'remove_unnecessary_styles' );
For Remove only themes styles and scripts you can try below :
function remove_all_scripts_from_theme() {
global $wp_scripts;
# remove all js
// $wp_scripts->queue = array();
foreach( $wp_scripts->queue as $handle ) {
if (strpos($wp_scripts->registered[$handle]->src, '/themes/') !== false) {
# dequeue js
wp_dequeue_script( $handle );
# deregister js
wp_deregister_script( $handle);
}
}
}
add_action('wp_print_scripts', 'remove_all_scripts_from_theme', 100);
function remove_all_styles_from_theme() {
global $wp_styles;
# remove all css
// $wp_styles->queue = array();
foreach( $wp_styles->queue as $handle ) {
if (strpos($wp_styles->registered[$handle]->src, '/themes/') !== false) {
# dequeue js
wp_dequeue_style( $handle );
# deregister js
wp_deregister_style( $handle);
}
}
}
add_action('wp_print_styles', 'remove_all_styles_from_theme', 100);
let me know if it works or not. i have tested this code. it is works like charms :-)
Thank you!
If there is no theme, the theme's function.php will never run so this should work as expected:
function myprefix_disable_theme_load() {
return "";
}
add_filter("template_directory", "myprefix_disable_theme_load", 1, 0);
add_filter("stylesheet_directory", "myprefix_disable_theme_load", 1, 0);
But be aware of this remove all functionality from the theme which we want in this case.
You can use wp_print_scripts to remove scripts and styles enqueued from plugins or parent theme. Please see the demo code:
/**
* Dequeue the Parent Theme and Plugins scripts and styles
*/
function custom_dequeue_script() {
wp_dequeue_script( 'script-handle' );
wp_dequeue_style('script-handle');
wp_deregister_style('script-handle');
}
add_action( 'wp_print_scripts', 'custom_dequeue_script', 100 );
Please try this new code to remove scripts and styles from activated theme:
function remove_theme_all_scripts() {
global $wp_scripts;
$stylesheet_uri = get_stylesheet_directory_uri();
$new_scripts_list = array();
foreach( $wp_scripts->queue as $handle ) {
$obj = $wp_scripts->registered[$handle];
$obj_handle = $obj->handle;
$obj_uri = $obj->src;
if ( strpos( $obj_uri, $stylesheet_uri ) === 0 ) {
//Do Nothing
} else {
$new_scripts_list[] = $obj_handle;
}
}
$wp_scripts->queue = $new_scripts_list;
}
add_action('wp_print_scripts', 'remove_theme_all_scripts', 100);
function remove_theme_all_styles() {
global $wp_styles;
$stylesheet_uri = get_stylesheet_directory_uri();
$new_styles_list = array();
foreach( $wp_styles->queue as $handle ) {
$obj = $wp_styles->registered[$handle];
$obj_handle = $obj->handle;
$obj_uri = $obj->src;
if ( strpos( $obj_uri, $stylesheet_uri ) === 0 ) {
//Do Nothing
} else {
$new_styles_list[] = $obj_handle;
}
}
$wp_styles->queue = $new_styles_list;
}
add_action('wp_print_styles', 'remove_theme_all_styles', 100);