10

How can I get all enqueued styles or scripts and then deregister them all at once?

4 Answers 4

27

I hope you know what you are doing. You can use the wp_print_styles and wp_print_scripts action hooks and then get the global $wp_styles and $wp_scripts object variables in their respective hooks.

The "registered" attribute lists registered scripts and the "queue" attribute lists enqueued scripts on both of the above objects.

An example code to empty the scripts and style queue.

function pm_remove_all_scripts() {
    global $wp_scripts;
    $wp_scripts->queue = array();
}
add_action('wp_print_scripts', 'pm_remove_all_scripts', 100);

function pm_remove_all_styles() {
    global $wp_styles;
    $wp_styles->queue = array();
}
add_action('wp_print_styles', 'pm_remove_all_styles', 100);
5
  • Lol, i'm learning what i'm doing... hehe. But thanks, that's exactly what I needed. I'm trying to minify all scripts, attach them together in one file, and enqueue only the new file. :D
    – trusktr
    May 13, 2011 at 19:42
  • I know there's plugins for this already, but they always end up breaking my site someway or another, so doing it manually is an option. :D
    – trusktr
    May 13, 2011 at 19:43
  • So why tell it to accept 100 arguments exactly?
    – trusktr
    May 13, 2011 at 19:46
  • 1
    That 100 is actually priority so that the hooked functions are called after all the functions hooked to wp_print_styles or wp_print_scripts are called. May 13, 2011 at 20:18
  • Oh, gotcha! To give plugins, etc, a chance to register all their scripts.
    – trusktr
    May 13, 2011 at 22:28
5

You can also do it on a per basis by finding the handlers being called upon, search for wp_enqueue_style or wp_enqueue_script you can deregister them like this on your functions.php

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );

function my_deregister_styles() {
    wp_deregister_style( 'some-css' );
}


add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );

function my_deregister_javascript() {
    wp_deregister_script( 'tutorials-js' );
    wp_deregister_script( 'gsc_dialog' );
    wp_deregister_script( 'gsc_jquery' );
}

Hameedullah solution is better, however if you run into problems due to some scripts not loading give the above a shot.

0
1

Hello you can also remove all these script, and wordpress will work correctly. Since Wordpress lets you remove all the script, and the ones he needs can not be removed. The audio script and others are script extras, so this will not cause problems in your wordpress.

/**
 * Dequeue the Parent Theme scripts or plugin.
 *
 * Hooked to the wp_print_scripts action, with a late priority (100),
 * so that it is after the script was enqueued.
 */
function my_site_WI_dequeue_script() {
 wp_dequeue_script( 'comment-reply' ); //If you're using disqus, etc.
 wp_dequeue_script( 'jquery_ui' ); //jQuery UI, no thanks!
 wp_dequeue_script( 'fancybox' ); //Nah, I use FooBox
 wp_dequeue_script( 'wait_for_images' );
 wp_dequeue_script( 'jquery_easing' );
 wp_dequeue_script( 'swipe' );
 wp_dequeue_script( 'waypoints' );
}

add_action( 'wp_print_scripts', 'my_site_WI_dequeue_script', 99 );
1

Hameedullah's method is useful, however if you only want this to work in the front end of the site (eg:not your WordPress dashboard) you need to add a conditional to force it to bail early.

I'm using a check mentioned in this question along with is_admin() to do this.

function pm_remove_all_scripts(){
  if(in_array($GLOBALS['pagenow'], ['wp-login.php', 'wp-register.php']) || is_admin()) return; //Bail early if we're
  global $wp_scripts;
  $wp_scripts->queue = array();
}

add_action('wp_print_scripts', 'pm_remove_all_scripts', 100);

function pm_remove_all_styles(){
  if(in_array($GLOBALS['pagenow'], ['wp-login.php', 'wp-register.php']) || is_admin()) return; //Bail early if we're

  global $wp_styles;
  $wp_styles->queue = array();
}

add_action('wp_print_styles', 'pm_remove_all_styles', 100);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.