1

I want to dequeue all styles and scripts that are loaded on the front-end (EG. not the admin panel) by default.

I found this function, but am not sure how to utilize it to accomplish my goal.

I'm seeing a ton of assets that I don't need on the front end, loaded by WP core:

For example:

  1. backbone.js
  2. jquery UI
  3. jquery UI datepicker
  4. 5 different mediaelement assets (js + css)
  5. underscore.js
  6. wp-embed js
  7. wp-util js
  • 1
    Most of these assets are not loaded by core by default. These assets are most likely added by either your theme or some plugin you have installed. – Howdy_McGee Oct 20 '16 at 14:50
  • Are you sure about that? wp-embed is definitely loaded by default: make.wordpress.org/core/2015/10/28/… – Ken Prince Oct 20 '16 at 14:55
  • You're right, let me rephase. In a vanilla theme there's only a few scripts that WordPress loads.wp-embed.min.js is one and if you have the admin bar enabled for your user it will also load dashicons, admin-bar css and admin-bar-js. WordPress has the ability to enqueue these things but it's up to the theme and plugin developers to decide what they need to enqueue. You can test this yourself by making a simple theme ( index.php and style.css ) which will show you a vanilla default. – Howdy_McGee Oct 20 '16 at 15:07
  • Note that a number of those scripts aren't loaded by default on the frontend when logged out, plugins and themes may be enquing them for various reasons, and you could break things by dequeuing them – Tom J Nowell Oct 20 '16 at 15:52
2

This will do the trick for you, assuming that you don't have any custom extra assets loading from the /wp-admin/ directory in the frontend.

It takes the $wp_scripts and $wp_styles globals, iterates through the registered resources and deregisteres the resources which have a source directory not containing '/wp-admin/'.

function my_deregister_scripts_and_styles() {
    global $wp_scripts, $wp_styles;

    foreach($wp_scripts->registered as $registered)
        if(strpos($registered->src,'/wp-admin/')===FALSE)
            wp_deregister_script($registered->handle);

    foreach($wp_styles->registered as $registered)
        if(strpos($registered->src,'/wp-admin/')===FALSE)
            wp_deregister_style($registered->handle);
}
add_action( 'wp_enqueue_scripts', 'my_deregister_scripts_and_styles');
1

I'm not sure what more you need that the example there, and remember that some scripts are needed for stuff like the admin bar and are not enqueued if you are not logged in.

function wpdocs_dequeue_script() {
   wp_dequeue_script( 'jquery-ui-core' );
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );

This will dequeue the jquery-ui-core js. Adding more lines of 'wp_dequeue_script' with the JS you want to dequeue will remove them aswel. You can find all the handles trough a dump of $wp_scripts.

<?php global $wp_scripts; var_dump($wp_scripts); ?>
  • Worth mentioning all of the assets I noted are loaded by default on the front end for all users, even user who are not logged in. Thanks. I also found this article which covers deregistering as well: inspirationalpixels.com/tutorials/… – Ken Prince Oct 20 '16 at 14:52
  • 3
    @KJPrince deregistering and dequeing are not the same thing, to dequeue is to say don't mow the lawn, to deregister is to forget what a lawn is, you want to dequeue and you want to stick to that terminology else you'll get answers that aren't relevant – Tom J Nowell Oct 20 '16 at 15:51
  • 1
    @TomJNowell +1 for reminding me I need to mow my lawn (or at least rake up my leaves). – Pat J Oct 20 '16 at 16:38
  • Uh oh, I deregestered it...what's a lawn? – socki03 Oct 20 '16 at 18:28
  • The wp_print_scripts hook seems to be too late; not working for me. Use the wp_enqueue_scripts hook as stated by dalv lexa. – Corgalore Jul 21 '17 at 15:37

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.