WP_Verify_Nonce()为什么返回FALSE?

时间:2017-12-08 作者:Alex

我试图创建主题选项管理页面(没有设置API)

在这段代码中,我想知道为什么wp\\u verify\\u nonce return false:

main file

<?php

add_action(\'admin_menu\', \'awesome_page_create\');
function awesome_page_create() {
    $page_title = \'My Awesome Admin Page\';
    $menu_title = \'Awesome Admin Page\';
    $capability = \'edit_posts\';
    $menu_slug = \'awesome_page\';
    $function = \'my_awesome_page_display\';
    $icon_url = \'\';
    $position = 24;

    add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
}
function my_awesome_page_display() {
    if (!current_user_can(\'manage_options\')) {
        wp_die(\'Unauthorized user\');
    }

    if (! wp_verify_nonce( \'_wp_nonce\', \'wpshout_option_page_example_action\' )) {
        wp_die(\'Nonce verification failed\');
    }

    if (isset($_POST[\'awesome_text\'])) {
        update_option(\'awesome_text\', $_POST[\'awesome_text\']);
        $value = $_POST[\'awesome_text\'];
    }

    $value = get_option(\'awesome_text\', \'hey-ho\');

    include \'form-file.php\';
}
$value = get_option(\'awesome_text\');
if (FALSE === $value) {
    $value = \'hey-ho\';
}

form-file


<h1>My Awesome Settings Page</h1>

<form method="POST">
    <label for="awesome_text">Awesome Text</label>
    <input type="text" name="awesome_text" id="awesome_text" value="<?php echo $value; ?>" />
    <?php echo wp_nonce_field( \'wpshout_option_page_example_action\' ); ?>
    <input type="submit" value="Save" class="button button-primary button-large" />
</form>

2 个回复
SO网友:Frank P. Walentynowicz

修订后的代码

if(is_admin()) {
    add_action(\'admin_menu\', \'awesome_page_create\');
    function awesome_page_create() {
        $page_title = \'My Awesome Admin Page\';
        $menu_title = \'Awesome Admin Page\';
        $capability = \'edit_posts\';
        $menu_slug = \'awesome_page\';
        $function = \'my_awesome_page_display\';
        $icon_url = \'\';
        $position = 24;
        add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position);
    }
    function my_awesome_page_display() {
        if (!current_user_can(\'manage_options\')) {
            wp_die(\'Unauthorized user\');
        }
        $value = get_option(\'awesome_text\', \'\');
?>
        <h1>My Awesome Settings Page</h1>
        <form method="POST">
            <label for="awesome_text">Awesome Text</label>
            <input type="text" name="awesome_text" id="awesome_text" value="<?php echo $value; ?>" />
            <?php wp_nonce_field(\'wpshout_option_page_example_action\', \'awesome_nonce\', false); ?>
            <input type="submit" value="Save" class="button button-primary button-large" />
        </form>
<?php
    }
    add_action(\'init\', \'process_my_awesome_form_data\');
    function process_my_awesome_form_data() {
        if(isset($_REQUEST[\'awesome_nonce\'])) {
            if(wp_verify_nonce($_REQUEST[\'awesome_nonce\'], \'wpshout_option_page_example_action\')) {
                if (isset($_POST[\'awesome_text\'])) {
                    update_option(\'awesome_text\', $_POST[\'awesome_text\']);
                }
            } else {
                echo \'nonce verification failed\';
            }
        }
    }
}
解释if(is_admin()) { 确保您在正确的上下文中创建了管理菜单。

而不是include \'form-file.php\';, 表单嵌入到my_awesome_page_display 函数,使其更易于调试。生产线<?php echo wp_nonce_field( \'wpshout_option_page_example_action\' ); ?> 表单中的定义更改为<?php wp_nonce_field(\'wpshout_option_page_example_action\', \'awsome_nonce\', false); ?>. 您的原始行为输出两个相同的隐藏输入字段nonce, 一个,因为echo, 第二,因为第四个参数的默认值为true (这意味着-打印此行)。

不要在中处理表单提交my_awesome_page_display 作用相反,使用init 钩子以添加表单的提交处理功能process_my_awsome_form_data. 在该功能中,$_REQUEST[\'awsome_nonce\'] 用于检索nonce 为了验证,$_POST[\'awesome_text\'] 用于检索文本输入的值。

SO网友:Elex

我想是为了你wp_nonce_field 您必须使用第二个参数(自定义nonce的字段名)。

<?php echo wp_nonce_field( \'wpshout_option_page_example_action\', \'wpshout_option_page_nonce_field\' ); ?>
那么当你使用if 暂时使用声明:

if (! wp_verify_nonce( $_POST[\'wpshout_option_page_nonce_field\'], \'wpshout_option_page_example_action\' )) {
    wp_die(\'Nonce verification failed\');
}
试试看,我觉得没关系:)

结束

相关推荐

Virtual Pages plugins

我很难让插件正常工作Virtual Pages (WordPress插件可简化虚拟页面的创建)我确实进行了编辑,根据查询创建了一个循环。add_action( \'gm_virtual_pages\', function( $controller ) { /* Creating virtuals pages for companies */ $args = array( \'post_type\' => array(\'companies\',), \'post_status\'