Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress wp_create_nonce()未在自定义插件中创建值_Wordpress_Nonce - Fatal编程技术网

Wordpress wp_create_nonce()未在自定义插件中创建值

Wordpress wp_create_nonce()未在自定义插件中创建值,wordpress,nonce,Wordpress,Nonce,我正在通过以下步骤创建一个用于用户注册的自定义插件: 创建我的HTML表单字段的函数 function render_registration_form() { ob_start(); ... <input type="hidden" id="register_nonce" name="register_nonce" value="<?php wp_create_nonce('generate-nonce'); ?>" /> //Above lin

我正在通过以下步骤创建一个用于用户注册的自定义插件:

创建我的HTML表单字段的函数

function render_registration_form() {
   ob_start();
   ...
   <input type="hidden" id="register_nonce" name="register_nonce" value="<?php wp_create_nonce('generate-nonce'); ?>" />
   //Above line does not create a nonce at all
   ...
   return ob_get_clean();
}
add_-shortcode('register-user','custom_-user_-registration_-form')

验证新用户并将其与meta一起添加到数据库的函数:

function validate_and_create_user() {
   if(isset($_POST['txt_username']) && wp_verify_nonce($_POST['register_nonce'], 'register-nonce')) {
   $user_data = array(
     'user_login' => $loginid,
     'user_pass' => $password,
     'user_nicename' => $first_name,
     'display_name' => $first_name . ' ' . $last_name,
     'user_email' => $email,
     'first_name' => $first_name,
     'last_name' => $last_name,
     'user_registered' => date('Y-m-d H:i:s'),
     'role' => 'subscriber'
     );

     $new_user_id = wp_insert_user($user_data);

     ...
}

add_action('init', 'validate_and_create_user');
由于表单内部未创建任何
nonce
值,因此检查
wp\u verify\u nonce($\u POST['register\u nonce','register nonce')
始终失败,并且数据库中未保存任何用户数据

可插拔功能不再添加到WordPress核心。所有新函数都在其输出上使用过滤器,以允许对其功能进行类似的重写

我是WordPress的新手。不知道怎么解决这个问题!任何帮助都会对我大有帮助。

试试这个:

wp_nonce_field('register_nonce');
在输入的地方

和用于检索nonce,请使用以下命令

$retrieved_nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($retrieved_nonce, 'register_nonce')) die( 'Failed security check' );

伟大的工作得很有魅力:)完美而中肯的答案。非常感谢。但是,问题是在文本框中注册nonce字段和指定nonce值之间有什么区别?为什么我的方法不起作用?因为wp_create_nonce('my nonce')它自己创建一个元素,所以不需要使用inputI,我明白这一点。但是为什么
wp\u create\u nonce
不能与自定义输入一起工作,这一直困扰着我:)如果你能解释一下,那就太好了。试试这个!我想你忘了加回声了
$retrieved_nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($retrieved_nonce, 'register_nonce')) die( 'Failed security check' );