WP_INSERT_USER函数未将密码字段添加到数据库

时间:2014-04-19 作者:poonam

我有一个项目,其中我做了一个自定义注册页面,但问题是,我的密码字段值没有添加到数据库中,而电子邮件和用户名正确添加。我不明白为什么没有存储密码值。这是我的php代码。

    $pwd1 = $wpdb->escape(trim($_POST[\'pwd1\']));
    $pwd2 = $wpdb->escape(trim($_POST[\'pwd2\']));
    $first_name = $wpdb->escape(trim($_POST[\'first_name\']));
    $last_name = $wpdb->escape(trim($_POST[\'last_name\']));
    $email = $wpdb->escape(trim($_POST[\'email\']));
    $username = $wpdb->escape(trim($_POST[\'username\']));

           if( $email == "" || $pwd1 == "" || $pwd2 == "" || $username == "" ||                      $first_name              == "" || $last_name == "") {
        $err = \'Please don\\\'t leave the required fields.\';
    } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $err = \'Invalid email address.\';
    } else if(email_exists($email) ) {
        $err = \'Email already exist.\';
    } else if($pwd1 <> $pwd2 ){
        $err = \'Password do not match.\';  

    } else {

        $user_id = wp_insert_user( array (\'user_login\' => apply_filters(\'pre_user_user_login\', $username), \'user_pass\' => apply_filters(\'pre_user_user_pass\', $pwd1 ), \'user_email\' => apply_filters(\'pre_user_user_email\', $email), \'role\' => \'subscriber\' ) );
        if( is_wp_error($user_id) ) {
            $err = \'Error on user creation.\';
        } else {
            do_action(\'user_register\', $user_id);

            $success = \'You\\\'re successfully register\';
        }

    }
这是我的html代码

           <ul style="margin-left:100px;"type="disk">
      <li><label style="margin-right:30px; margin-top:20px;">Username:</label>
       <input type="text" id="username" name="username" required="required" <span        id="msg_username"></span></li>
          <li><label style="margin-right:30px;">First Name:</label>
           <input type="text" name="first_name" ></li>
          <li><label style="margin-right:30px;">Last Name:</label>
      <input type="text" name="last_name" ></li>
      <li><label style="margin-right:60px;">Email:</label>
      <input type="text" name="email" ></li>
    <li><label style="margin-right:30px;">Password:</label>
    <input type="password" name="pwd1" ></li>
    <li><label style="margin-right:30px;">Confirm Password:</label>
    <input type="password" name="pwd2" ></li>

        </ul>
请告诉我这有什么问题。

4 个回复
SO网友:gmazzap

你不需要打电话\'pre_user_*\' 过滤器,由WordPress内部调用wp_insert_user.

我还建议使用phpfilter_inputfilter_input_array 清理表单输入。

示例代码:

$args = array(
  \'pwd1\'       => FILTER_SANITIZE_STRING,
  \'pwd2\'       => FILTER_SANITIZE_STRING,
  \'first_name\' => FILTER_SANITIZE_STRING,
  \'last_name\'  => FILTER_SANITIZE_STRING,
  \'username\'   => FILTER_SANITIZE_STRING,
  \'email\'      => FILTER_SANITIZE_STRING
);
$form_data = filter_input_array( INPUT_POST, $args, TRUE );
$keys = array_keys($args);
$required = TRUE;
while ( ! empty( $keys ) && $required ) {
  $key = array_shift( $keys );
  if ( empty( $form_data[$key] ) ) $required = FALSE; 
}
if( ! $required ) {
  $err = \'Please don\\\'t leave the required fields.\';
} else if( ! filter_var( $form_data[\'email\'], FILTER_VALIDATE_EMAIL ) ) {
  $err = \'Invalid email address.\';
} else if( $form_data[\'pwd1\'] !== $form_data[\'pwd2\'] ){
  $err = \'Password do not match.\';  
} else {
  $user_data = array(
    \'user_login\' => $form_data[\'username\'],
    \'user_pass\'  => $form_data[\'pwd1\'],
    \'user_email\' => $form_data[\'email\'],
    \'role\'       => \'subscriber\'
  );
  $user_id = wp_insert_user( $user_data );
  if( is_wp_error( $user_id ) ) {
    $err = \'Error on user creation: \' . $user_id->get_error_message();
  } else {
    do_action(\'user_register\', $user_id);
    $success = \'You\\\'re successfully register\';
  }
}

SO网友:Sormano

到目前为止我可以看到你apply filters 是不对的,他们不应该有替身\'pre_user_user_\' 但仅限于\'pre_user_\'

希望这有帮助。

SO网友:Sumit

首先需要从generate password(生成密码)函数生成密码,实际上密码是插入的,并且仅当密码在插入之前已加密时才起作用。应该从md5函数加密密码。

SO网友:Rahul Jalavadiya

这是我的密码

i sent $password as it is as user fill in the form no aditional requirements.

$userdata = array(
        \'user_login\'  =>  $username,
        \'user_pass\'   =>  $password,
        \'user_nicename\' => str_replace(\' \',\'-\',$display_name_appearing_on_website),
        \'user_email\' => $email_address,
        \'display_name\' => $display_name_appearing_on_website,
        \'description\' =>$company_info
        );
        $user_id = wp_insert_user($userdata);

结束

相关推荐