wp_deregister_script()WP 2.1.0

Deletes a previously registered script.

To enqueue a new script, use the function wp_register_script(), and to output it in the code, use wp_enqueue_script(). Usually, these functions are used together and hooked to actions:

Use wp_dequeue_script() to remove the script from the queue.

No Hooks.

Returns

null. Returns nothing.

Usage

wp_deregister_script( $handle );
$handle(string) (required)
The name of the script to be deleted (de-registered).

Examples

1

#1 Register your jQuery script

Now, suppose we want to change the link from where the jQuery script will be downloaded, we will use google CDN, i.e. the script will be downloaded from google repository:

// jQuery registration
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

function my_scripts_method() {

	// cancel the registered jQuery
	// instead of "jquery-core" you can write "jquery", then jquery-migrate will also be cancelled
	wp_deregister_script( 'jquery-core' );
	wp_register_script( 'jquery-core', '//ajax.googleapis.com/ajax/libs/jquery/3/jquery.min.js');

	wp_enqueue_script( 'jquery' );
} 
0

#2 Removing the jQuery script

Suppose we need to remove the basic registration of the jQuery script. Then use the following code in the themes functions.php file:

wp_deregister_script( 'jquery' );
0

#3 Deregistering will not dequeue the script handle in the strict sense.

You may use wp_deregister_script ( 'script-handle' ); followed by wp_register_script() if 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.

Notes

Changelog

Since 2.1.0 Introduced.

wp_deregister_script() code WP 6.9

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