0

I have a project in which i made a custom registration page but the problem is that my password field value not added to the database while email and username properly added. I could not understand why password value not stored. this is my php code.

    $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';
        }

    }

this is my html code

           <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>

please tell me what is wrong with this.

4 Answers 4

1

You don't need to call 'pre_user_*' filters, that are called by WordPress inside wp_insert_user.

Also I suggest to use php filter_input or filter_input_array to sanitize form input.

Example code:

$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';
  }
}
0

First need to generate the password from generate password function actually password is inserted and working only when if the password is encrypted before inserting the should be encrypt the password from md5 function.

0

here is my code work like charm

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);
0

So far I can see your apply filters are not correct, they shouldn't have a double 'pre_user_user_' but only 'pre_user_'

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.