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:
- backbone.js
- jquery UI
- jquery UI datepicker
- 5 different mediaelement assets (js + css)
- underscore.js
- wp-embed js
- 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:
- backbone.js
- jquery UI
- jquery UI datepicker
- 5 different mediaelement assets (js + css)
- underscore.js
- wp-embed js
- wp-util js
4 Answers
Reset to default 2This 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
版权声明:本文标题:plugins - How to Dequeue All WordPress Assets 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736669695a1946850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp-embed.min.js
is one and if you have the admin bar enabled for your user it will also loaddashicons
,admin-bar
css andadmin-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
andstyle.css
) which will show you a vanilla default. – Howdy_McGee ♦ Commented Oct 20, 2016 at 15:07