Vcoder
Vcoder

Reputation: 124

Cannot set user password in wordpress

i have tried everything i could find to set the user password on registration, but no success... I have the fields showing up, the verification(if the passwords match etc) i print them on screen, i print the userid on screen so every argument needed is there, but the function doesn't seem to work at all... This doesn't work...

$newpassword = "zzzzzz";

update_user_meta($user_id, 'user_pass', $newpassword);

This doesn't work either...

add_action( 'user_register', 'ts_register_extra_fields', 10 );
function ts_register_extra_fields($user_id, $password='11',$meta = array()){
    $userdata = array();
      if ( $_POST['password'] !== '' ) {
       $userdata['user_pass'] = $_POST['password'];
    }
    $new_user_id = wp_update_user( $userdata );
  }

My customer needs this for tomorrow, so I'm totally lost by now, i have no clue on why it's not working...

Forgot to add, all this code is added in the functions.php of my theme. (It gets into it as i already said that i post the variables on screen).

add_action( 'user_register', 'ts_register_extra_fields', 100 );

function ts_register_extra_fields( $user_id, $password = '', $meta = array() ) {

    $userdata = array();

    $userdata['ID'] = $user_id;

    $userdata['contacto'] = $_POST['contacto'];

    $userdata['nif'] = $_POST['nif'];

     if ( $_POST['password'] !== '' ) {
         $userdata['user_pass'] = $_POST['password'];
         echo "im in";
     }
     $new_user_id = wp_insert_user( $userdata );

     echo "id-".$userdata['ID'];

     echo "contacto-".$userdata['contacto'];

     echo "nif-".$userdata['nif'];

     echo "pass-".$userdata['user_pass'];
}

All those echos output the correct data... for example id = 195 the next time i try 196 etc... contacto and nif show the data that i input in the custom registration field and the pass also shows the data that i had inputed in the custom registration field password...

Upvotes: 0

Views: 1823

Answers (2)

Vcoder
Vcoder

Reputation: 124

A HA! Found the error. I have another plugin installed called "New User Aprovement" which required an administrator aprovement in order for the user to login. That plugin when the administrator accepted the user to login, generated another password (to be able to send the password to the user in a readable mode), invalidating the password update that i made when the user registered(because it generated a random password after the admin accept).

I found this by disabling the plugin and testing the functions.php. It did work. In order to make them both work i just erased the code in the plugin that generated a random password. Although the user doesn't receive the account summary via email. It works for my needs.

Best Regards, Vcoder

Upvotes: 1

Just Thomas Misund
Just Thomas Misund

Reputation: 539

First of all, I think WordPress is using MD5 encryption for passwords.

$hash = wp_hash_password( $newpassword );
// then wp_update_user with $hash as the user_pass value

Secondly, you shouldn't send passwords in clear text over the Internet. If you can encrypt the password with javascript before you send it, it would probably be a lot safer.

At last, give a shot at updating an existing user by specifying ID in wp_update_user.

Upvotes: 1

Related Questions