Mat*_*tto 6 php wordpress integrity bootstrap-4
我正在尝试在WordPress上使用wp_register_script和wp_enqueue_script FUNCTION来排队一个脚本,该脚本有两个属性:"integrity"和"crossorigin".
通常我使用PHP,我的代码如下:
wp_register_script('jquery', 'http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://code.jquery.com/jquery-3.1.1.slim.min.js', false, null);
wp_enqueue_script('jquery');
Run Code Online (Sandbox Code Playgroud)
使用任何其他脚本.wp_register_script有五个参数(在本例中为4个)$ handle,$ src,$ deps,$ ver($ media).我想知道我可以在哪里添加这两个属性.我已经尝试过了:
wp_register_script('jquery', 'http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://code.jquery.com/jquery-3.1.1.slim.min.js'.'integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n"', false, null);
wp_enqueue_script('jquery');
Run Code Online (Sandbox Code Playgroud)
但它没有用.
谁有同样的问题?这是来自bootstrap 4的原始脚本,它也具有相同属性(完整性和交叉源)的bootstrap和tether,所以,因为它是非常新的,所以任何类型的帮助都将非常受欢迎.
che*_*tin 16
我只是意外地解决了这个问题.我不得不在古代Internet Explorer版本的某些CSS文件上使用条件,并且我想我也可以尝试将数组放在我使用的相同wp_script_add_data函数中.有用!
wp_register_script('jquery3', 'https://code.jquery.com/jquery-3.3.1.min.js', array(), '3.3.1', true); // jQuery v3
wp_enqueue_script('jquery3');
wp_script_add_data( 'jquery3', array( 'integrity', 'crossorigin' ) , array( 'sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=', 'anonymous' ) );
Run Code Online (Sandbox Code Playgroud)
参考:https://developer.wordpress.org/reference/functions/wp_script_add_data/#parameters
为了完整性:如果添加jQuery v3,您还需要迁移v3:http://jquery.com/download/
您可以使用script_loader_tag挂钩(主要部分实际上不是我的代码,但老实说我不记得我在哪里得到它,可能在SO或WP Stack Exchange上的某处):
add_filter( 'script_loader_tag', 'add_attribs_to_scripts', 10, 3 );
function add_attribs_to_scripts( $tag, $handle, $src ) {
// The handles of the enqueued scripts we want to defer
$async_scripts = array(
'jquery-migrate',
'sharethis',
);
$defer_scripts = array(
'contact-form-7',
'jquery-form',
'wpdm-bootstrap',
'frontjs',
'jquery-choosen',
'fancybox',
'jquery-colorbox',
'search'
);
$jquery = array(
'jquery'
);
if ( in_array( $handle, $defer_scripts ) ) {
return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
}
if ( in_array( $handle, $async_scripts ) ) {
return '<script src="' . $src . '" async="async" type="text/javascript"></script>' . "\n";
}
if ( in_array( $handle, $jquery ) ) {
return '<script src="' . $src . '" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous" type="text/javascript"></script>' . "\n";
}
return $tag;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6736 次 |
| 最近记录: |