1

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:

  1. I don't know what theme will be used.
  2. I need to dequeue / deregister only the styles and scripts from the theme and not from other plugins.
  • Create the plugin, list all enqueued scripts and dequeue any of them. Take care about which plugins loads earlier and load your plugin later. – Max Yudin Jun 18 at 5:15
  • But i need to dequeue / deregister only the styles and scripts from the theme and not from other plugins – user315338 Jun 18 at 5:25
  • Plugins get loaded before theme, so hook your plugin to the after_setup_theme – Max Yudin Jun 18 at 6:08
  • See @Jacob Peattie' great answer. – Max Yudin Jun 18 at 6:10
1

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.

  • Wow, great idea!!! Thanks!!! – user315338 Jun 18 at 8:01
0

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!

  • 1
    But it suppose to be working no matter what theme is installed, this way i have to provide the scripts and styles which i don't know. – user315338 Jun 18 at 5:19
  • of course you have to provide the name of styles and scripts which you want to remove. which style and scripts you want to remove you can find that names in view source or en developer tools inspect element of webpage. – Chetan Vaghela Jun 18 at 5:25
  • I Think you didn't really get the question, i edited the question to be more clear, please read again. Thank you very much! – user315338 Jun 18 at 5:31
  • i have update the answer. please check it out. – Chetan Vaghela Jun 18 at 6:04
-1

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);
  • 1
    But it suppose to be working no matter what theme is installed, this way i have to provide the scripts and styles which i don't know. – user315338 Jun 18 at 5:21
  • You need to add proper script handle to remove/dequeue and also if it is not working then you need to change the priority in the hook. – Bhupen Jun 18 at 5:22
  • You can try this new code given above to remove the activated theme's scripts and styles. – Bhupen Jun 18 at 5:54

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.