删除已注册的脚本。
原型
wp_deregister_script( string $handle )
描述
注意:有一些有意的安全措施可以防止关键管理脚本(如jQuery核心)被取消注册。
参考:
- WP_Dependencies::remove()
参数
$handle
(string)
(Required)
要删除的脚本的名称。
源文件
路径:wp-includes/functions.wp-scripts.php
<?php
...
function wp_deregister_script( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
/**
* 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' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
) {
$no = 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, $no ) ) {
$message = 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>'
);
_doing_it_wrong( __FUNCTION__, $message, '3.6.0' );
return;
}
}
wp_scripts()->remove( $handle );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_deregister_script/