!
也想出现在这里? 联系我们
广告位

【WP函数】wp_deregister_script()

前一篇 WordPress学习——wp_register_script()详解 说的是 WordPress 如何注册一个脚本。与之对应,这里的 wp_deregister_script() 则是用于移除注册的脚本的。不过出于安全考虑,一些 WordPress 核心的脚本是不允许使用该函数移除的。该函数的语法是:

wp_deregister_script( string $handle )

$handle:字符串(必填)。要移除的脚本名称。

该函数同样定义在 wp-includes/functions.wp-scripts.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/

给TA打赏
共{{data.count}}人
人已打赏
网站运维

【WP函数】wp_register_script()

2022-5-29 16:01:29

网站运维

去哪优化网站关键词(解答如何选择性价比更高的词)

2022-7-15 16:24:28

声明 本站上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。无意侵害您的权益,请发送邮件至 admin@s9h.cn 或点击右侧 私信:少羽 反馈,我们将尽快处理。
0 条回复A文章作者M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索