Removes a registered script.
Description
Note: there are intentional safeguards in place to prevent critical admin scripts, such as jQuery core, from being unregistered.
See also
Parameters
$handlestringrequired- Name of the script to be removed.
Source
function wp_deregister_script( $handle ) {
global $pagenow;
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
/**
* Do not allow accidental or negligent de-registering of critical scripts in the admin.
* Show minimal remorse if the correct hook is used.
*/
$current_filter = current_filter();
if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
( 'wp-login.php' === $pagenow && 'login_enqueue_scripts' !== $current_filter )
) {
$not_allowed = array(
'jquery',
'jquery-core',
'jquery-migrate',
'jquery-ui-core',
'jquery-ui-accordion',
'jquery-ui-autocomplete',
'jquery-ui-button',
'jquery-ui-datepicker',
'jquery-ui-dialog',
'jquery-ui-draggable',
'jquery-ui-droppable',
'jquery-ui-menu',
'jquery-ui-mouse',
'jquery-ui-position',
'jquery-ui-progressbar',
'jquery-ui-resizable',
'jquery-ui-selectable',
'jquery-ui-slider',
'jquery-ui-sortable',
'jquery-ui-spinner',
'jquery-ui-tabs',
'jquery-ui-tooltip',
'jquery-ui-widget',
'underscore',
'backbone',
);
if ( in_array( $handle, $not_allowed, true ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: Script name, 2: wp_enqueue_scripts */
__( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
"<code>$handle</code>",
'<code>wp_enqueue_scripts</code>'
),
'3.6.0'
);
return;
}
}
wp_scripts()->remove( $handle );
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Deregistering will not dequeue the script handle in the strict sense.
You may use
wp_deregister_script ( 'script-handle' );followed bywp_register_scriptif you want to change the URL of an already enqueued script without changing the order in which it is enqueued, for example when a parent theme has not specified dependencies correctly.The “safeguards” mentioned in the description only apply in wp-admin (and the login screen) — not on the front end.
Protected handles (from the source): jquery, jquery-core,
jquery-migrate, jquery-ui-core, jquery-ui-accordion,
jquery-ui-autocomplete, jquery-ui-button, jquery-ui-datepicker,
jquery-ui-dialog, jquery-ui-draggable, jquery-ui-droppable,
jquery-ui-menu, jquery-ui-mouse, jquery-ui-position,
jquery-ui-progressbar, jquery-ui-resizable, jquery-ui-selectable,
jquery-ui-slider, jquery-ui-sortable, jquery-ui-spinner,
jquery-ui-tabs, jquery-ui-tooltip, jquery-ui-widget, underscore,
backbone.
Two things worth knowing:
1. Front end has NO protection. wp_deregister_script(‘jquery’) works fine outside wp-admin, with no warning — it will silently break anything on the page that depends on jQuery.
2. In wp-admin, calling it from the wrong hook doesn’t fail loudly. It just does nothing and fires _doing_it_wrong() , which is only visible if WP_DEBUG_LOG is enabled. Without that, it looks like your code ran, but the script is still registered.
// Silently ignored in wp-admin — wrong hook:
add_action( ‘admin_init’, function () {
wp_deregister_script( ‘jquery’ );
} );
// Correct hook for admin-side deregistration:
add_action( ‘admin_enqueue_scripts’, function () {
wp_deregister_script( ‘jquery’ );
} );
If jQuery (or any handle above) won’t deregister in wp-admin, check you’re using ‘admin_enqueue_scripts’ before assuming something else is broken.
Example