函数文档

wp_deregister_script()

💡 云策文档标注

概述

wp_deregister_script() 函数用于移除已注册的脚本,但包含安全机制以防止在管理后台意外注销关键脚本如 jQuery 核心。开发者需注意其使用场景和限制。

关键要点

  • 函数参数 $handle 为必需,指定要移除的脚本名称。
  • 在管理后台或登录页面注销关键脚本(如 jQuery、Underscore、Backbone 等)会受到限制,并可能触发 _doing_it_wrong() 警告。
  • 建议在正确钩子(如 wp_enqueue_scripts)中使用,以避免问题。
  • 注销脚本不会自动出队,可结合 wp_register_script() 重新注册以修改脚本 URL 而不影响入队顺序。

代码示例

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.' ),
					"$handle",
					'wp_enqueue_scripts'
				),
				'3.6.0'
			);
			return;
		}
	}

	wp_scripts()->remove( $handle );
}

注意事项

  • 在管理后台使用 wp_deregister_script() 时,需确保在 admin_enqueue_scripts 钩子中调用,否则可能无法注销关键脚本。
  • 注销脚本后,如需替换脚本源,可重新注册以避免依赖顺序问题。
  • 函数自 WordPress 2.1.0 版本引入,相关函数包括 wp_scripts()、current_filter() 等。

📄 原文内容

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.

User Contributed Notes

  1. Skip to note 3 content

    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.