wp_verify_nonce不工作,而wp_create_nonce工作

时间:2017-04-05 18:30:10

标签: wordpress

我很惊讶为什么wp_verify_nonce不起作用。它显示未定义的功能错误,我的wordpress版本是最新的。我附加了我的插件代码。请帮帮我

    add_shortcode('tw_safety_checklist_template','init_tw_safety_checklist');

    function init_tw_safety_checklist(){
        echo '<form method="post">
            <label>Name</label>
            <input type="hidden" name="tw_new_checklist_nonce" value="'.wp_create_nonce('tw_new_checklist_nonce').'"/>
            <input type="text" name="tw_name" />
            <input type="submit" name="submit" value="Submit"/>
        </form>';
    }

    if(isset($_POST['tw_new_checklist_nonce'])){
        tw_create_my_template();            
    } 

    function tw_create_my_template(){
        if(wp_verify_nonce($_POST['tw_new_checklist_nonce'],'tw-new-checklist-nonce'))
        {
            return 'Worked!';
        }
    }

1 个答案:

答案 0 :(得分:2)

问题在于UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0) pluggable功能。这意味着直到插件加载后才会声明它。由于您的wp_verify_nonce()语句在文件中是松散的,因此在插件加载时会执行该语句;因此,if(正确)尚未宣布。

您需要使用action hookwp_verify_nonce()语句移至add_action()。哪个钩子将取决于if函数的确切用途。你想做这样的事情:

tw_create_my_template()

注意,您希望将add_action('init','tw_create_my_template'); function tw_create_my_template(){ if( isset($_POST['tw_new_checklist_nonce']) && wp_verify_nonce($_POST['tw_new_checklist_nonce'],'tw-new-checklist-nonce')) { return 'Worked!'; } } 替换为适用于您的函数的任何钩子。 init对于插件初始化操作来说是相当典型的,但重要的是它是在plugins_loaded之后发生的事情。您可以按顺序here找到典型操作列表。

相关问题