未定义索引:name_of_your_nonce_field 在第一个基本示例中

Undefined index: name_of_your_nonce_field during first basic example with

当我使用 wp nonce 字段的第一步时,我尝试了 https://developer.wordpress.org/reference/functions/wp_nonce_field/

中的 "Basic Examples"

上面写着:"simplest implementation which omits all arguments"

在我的底部 htdocs/wp-content/plugins/abcd-plugin/abcd-plugin。php 我写道:

function hi_in_wp_head() {
    ?>
    <form name="f1">
        <input name="i1" value="hi_in_wp_head">
        <input type="submit" name="s1">
        <?php wp_nonce_field('name_of_your_action', 'name_of_your_nonce_field'); ?>
    </form>
    <?php
    if(wp_verify_nonce($_REQUEST['name_of_your_nonce_field'], 'name_of_your_action')){
        // Nonce is matched and valid. do whatever you want now.
    } else {
        // Invalid nonce. you can throw an error here.
        die("ups 19-02-28_17-09");
    }

}
function hi_in_footer() {
    echo '<h1>hi_in_footer</h1>';
}

完整来源: https://gist.github.com/f9f0a853f0a71c5a2055b88802a1010c

在网络浏览器中看起来像这样:

<meta name="generator" content="WordPress 5.0.3" />
    <form name="f1">
        <input name="i1" value="hi_in_wp_head">
        <input type="submit" name="s1">
        <input type="hidden" id="name_of_your_nonce_field" name="name_of_your_nonce_field" value="5a82357118" /><input type="hidden" name="_wp_http_referer" value="/wordpress/alecaddd-plugin.php" />    </form>
    <br />
<b>Notice</b>:  Undefined index: name_of_your_nonce_field in <b>G:\Bitnami\wordpress-5.0.3-2\apps\wordpress\htdocs\wp-content\plugins\alecaddd-plugin\alecaddd-plugin.php</b> on line <b>89</b><br />
ups 19-02-28_17-09

未定义的索引:name_of_your_nonce_field 在第一个基本示例中

不知道哪里出错了。我能做什么?

如错误消息所述,$_REQUEST['name_of_your_nonce_field'] 未设置。您需要确保它在使用前已设置好:

function hi_in_wp_head() {

    ?>
    <form name="f1">
        <input name="i1" value="hi_in_wp_head">
        <input type="submit" name="s1">
        <?php wp_nonce_field('name_of_your_action', 'name_of_your_nonce_field'); ?>
    </form>
    <?php
    if(isset($_REQUEST['name_of_your_nonce_field']) {
        if(wp_verify_nonce($_REQUEST['name_of_your_nonce_field'], 'name_of_your_action')){
            // Nonce is matched and valid. do whatever you want now.
        } else {
            // Invalid nonce. you can throw an error here.
            die("ups 19-02-28_17-09");
        }
    }

}

$_REQUEST['name_of_your_nonce_field'] 将在 提交表单后 设置。这就是您需要额外检查的原因。