将 nonce 添加到 <script>标签 [英] Adding nonce to <script> tag

查看:178
本文介绍了将 nonce 添加到 <script>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何向基于 WordPress 的页面上的所有 <script> 标签添加随机数.例如,请在下面找到一些代码:

I'm wondering how to add nonce to my all <script> tags on page based on WordPress. For example please find some code below:

$my_nonce = wp_create_nonce('nonce-'.rand());
$nonces = "Content-Security-Policy: script-src 'self nonce-".$my_nonce."'";
header( "{$nonces}");

wp_localize_script( 'my_loadmore', 'my_loadmore_params', array(
'ajaxurl' => site_url() . '\/wp-admin\/admin-ajax.php',
'posts' => json_encode( $wp_query->query_vars ), 
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
         'max_page' => $wp_query->max_num_pages,
) );`

我想要这样的东西:

<script nonce="nonce-value">...</script>

其中 nonce-value 是随机的.

这只是我需要它的地方之一,有什么想法如何将它全局添加到

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆