admin管理员组

文章数量:1125595

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

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
Share Improve this question asked Oct 20, 2016 at 14:39 Ken PrinceKen Prince 1431 silver badge10 bronze badges 4
  • 2 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 Commented Oct 20, 2016 at 14:50
  • Are you sure about that? wp-embed is definitely loaded by default: make.wordpress.org/core/2015/10/28/… – Ken Prince Commented Oct 20, 2016 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 Commented Oct 20, 2016 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 Commented Oct 20, 2016 at 15:52
Add a comment  | 

4 Answers 4

Reset to default 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');

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

To remove all wordpress assets

add_action('wp_default_scripts', function (&$scripts) {
    foreach (array_keys($scripts->registered) as $key) {
        wp_deregister_script($key);
        wp_dequeue_script($key);
    }
});

add_filter('wp_default_styles', function (&$styles) {
    foreach (array_keys($styles->registered) as $style) {
        wp_deregister_style($style);
        wp_dequeue_style($style);
    }
});

After that you can include your assets. But one thing you should know those scripts remove all WordPress and also other plugin assets so you should remove specific scripts only.

add_action('wp_default_scripts', function (&$scripts) {
    foreach (array_keys($scripts->registered) as $script) {
        if (in_array($script, ['backbone', 'jquery-core', 'wp-util'])) {
            wp_deregister_script($script);
            wp_dequeue_script($script);
        }
    }
});

add_filter('wp_default_styles', function (&$styles) {
    foreach (array_keys($styles->registered) as $style) {
        if (in_array($style, ['backbone', 'jquery-core'])) {
            wp_deregister_style($style);
            wp_dequeue_style($style);
        }
    }
});

You can find their $handle or key name in link or script tag id (I think everyone knows this ;) ) but without -js or -css

<script id="jquery-core-js" type="text/javascript" src="http://localhost/wp-includes/js/jquery/jquery.min.js?ver=3.7.1"></script>

Use all that in if (!is_admin()) {} for frontend usage

if ( !is_admin()) {
    add_action('wp_default_scripts', function (&$scripts) {
        foreach (array_keys($scripts->registered) as $script) {
            if (in_array($script, ['backbone', 'jquery-core', 'wp-util'])) {
                wp_deregister_script($script);
                wp_dequeue_script($script);
            }
        }
    });

    add_filter('wp_default_styles', function (&$styles) {
        foreach (array_keys($styles->registered) as $style) {
            if (in_array($style, ['backbone', 'jquery-core'])) {
                wp_deregister_style($style);
                wp_dequeue_style($style);
            }
        }
    });
}

Or Another way much easier:

add_action( 'wp_default_scripts', 'remove_js_ui_library' );
function remove_js_ui_library( &$scripts){
    if(!is_admin()){
        $scripts->remove(['jquery-ui-core']);
}

Is that easy, use dequeue on your functions.php file (inside your active theme). This way I dequeue 4 files:

// DEQUEUE GUTENBERG STYLES FOR FRONT
function my_deregister_scripts_and_styles() {
  wp_deregister_script('wp-util'); //deregister script
  wp_deregister_script('underscore'); 
  wp_dequeue_style( 'wp-block-library'); //deregister style
  wp_dequeue_style( 'wc-block-style' ); 
  wp_dequeue_style( 'wp-block-library-theme' );
}
add_action( 'wp_enqueue_scripts', 'my_deregister_scripts_and_styles', 999);

Try directly the asset name without it's extension (js / css) and if it doesn't work open the html inspector and try to use the id name of the script. For example: <link rel='stylesheet' id='resource-special-name' href='https://www.test.com/resource.css' type='text/css' />

If it doesn't work, then do a full search of the name (for example: block-style.css) and you will find a call as this:

`wp_enqueue_style( 'block-special-name', get_template_directory_uri() . '/css/vendor/block-style.css',  

本文标签: pluginsHow to Dequeue All WordPress Assets