Wordpress wp\u验证\u nonce不工作,而wp\u创建\u nonce工作
我很惊讶为什么wp\u-verify\u-nonce不起作用。它显示未定义的函数错误,我的wordpress版本是最新的。我附加我的插件代码。请帮帮我Wordpress wp\u验证\u nonce不工作,而wp\u创建\u nonce工作,wordpress,Wordpress,我很惊讶为什么wp\u-verify\u-nonce不起作用。它显示未定义的函数错误,我的wordpress版本是最新的。我附加我的插件代码。请帮帮我 add_shortcode('tw_safety_checklist_template','init_tw_safety_checklist'); function init_tw_safety_checklist(){ echo '<form method="post"> &
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!';
}
}
add_shortcode('tw_safety_checklist_template','init_tw_safety_checklist');
功能初始安全检查表(){
回声'
名称
';
}
如果(isset($_POST['tw_new_checklist\u nonce')){
tw_创建我的模板();
}
函数tw_create_my_template(){
如果(wp\u验证时间($\u POST['tw\u new\u checklist\u nonce','tw-new-checklist-nonce'))
{
返回“已工作!”;
}
}
问题在于wp\u verify\u nonce()
是一个函数。这意味着它在插件加载之后才被声明。因为你的if
语句在你的文件中是松散的,所以它是在你的插件加载时执行的;因此,wp\u verify\u nonce()
(正确)尚未声明
您需要将if
语句移动到using语句中。哪个钩子将取决于tw\u create\u my\u 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
对于插件初始化操作来说是相当典型的,但重要的是它是在初始化之后发生的。您可以按顺序找到典型操作的列表。问题在于wp\u verify\u nonce()
是一个函数。这意味着它在插件加载之后才被声明。因为你的if
语句在你的文件中是松散的,所以它是在你的插件加载时执行的;因此,wp\u verify\u nonce()
(正确)尚未声明
您需要将if
语句移动到using语句中。哪个钩子将取决于tw\u create\u my\u 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
对于插件初始化操作来说是相当典型的,但重要的是它是在初始化之后发生的。您可以按顺序找到典型操作的列表