我很惊讶为什么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!';
}
}发布于 2017-04-05 18:51:18
问题是wp_verify_nonce()是一个可封堵函数。这意味着它在加载插件之后才会声明。由于您的if语句在您的文件中是松散的,所以当您的插件加载时将执行它;因此,还没有声明wp_verify_nonce() (正确)。
您需要使用if将您的动作钩语句移动到add_action()中。哪个钩子将取决于您的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。init对于插件初始化操作来说是相当典型的,但重要的是它是在plugins_loaded之后发生的事情。您可以按照顺序找到典型操作的列表,这里。
https://stackoverflow.com/questions/43238990
复制相似问题