1

I need to rehash the password which the user entered during Wordpress registration (I use WooCommerce)

I'm successfully able to do this with the following:

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {

    if ( isset( $_POST['password'] ) ) {
        update_user_meta($user_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));
    }

}

However, I need to do this for 2 more occasions, profile update and reset password

I wrote:

function my_profile_update( $user_id ) {
    if ( ! isset( $_POST['password'] ) || '' == $_POST['password'] ) {
        return;
    }
    update_user_meta($user_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));
    $x = $_POST['password'];
    echo '<script language="javascript">';
    echo 'alert('.$x.')';
    echo '</script>';


    // password changed...
}
add_action( 'profile_update', 'my_profile_update' );

It doesn' work at all.

UPDATE

function my_profile_update( $user_id ) {

    update_user_meta($user_id, 'user_pass2', (string) $_POST['password']);
    // password changed...
}
add_action( 'profile_update', 'my_profile_update' );

It works but $_POST['password'] or $_POST['pass1'] returns nothing.

  • I think you want to use the personal_options_update action instead of (or in addition to) profile_update. – butlerblog Mar 29 at 2:34
  • the profile_update works right now, the problem is I cant access the $_POST['password'] or $_POST['pass1'], it returns nothing – Cameron A Mar 29 at 2:36
  • I updated the question – Cameron A Mar 29 at 2:38
  • You need to check the specific input field name that is being used for the screen in question. For example, if you're using the WooCommerce account page, the password is "password_1". – butlerblog Mar 29 at 2:40
  • Im exactly updating password via woocommerce account detail page – Cameron A Mar 29 at 2:44
1

Sometimes you have to look at the name of the input you're trying to pick up via $_POST. It's not always consistent across forms. In the case of the WooCommerce password change form, the input name for the new password field is 'password_1' so that's what you need to pick up via $_POST:

function my_profile_update( $user_id ) {

    if ( ! is_admin() ) {
       update_user_meta($user_id, 'user_pass2', (string) $_POST['password_1']);
    }
    // password changed...
}
add_action( 'profile_update', 'my_profile_update' );

When in doubt on the input tag name, use the browser inspector. While hovering your mouse over the field in question, right click and select "inspect". This will highlight the HTML for that field in the inspector and you can look at the value for "name". That's the value you need to use in $_POST.

Also note the addition of checking that the action is not run on the dashboard (admin) side (is_admin()). WooCommerce is using the same action hook as WP to consolidate (something that it sounds like you don't want to do).

  • now I have another problem , when I update any other info from Admin panel , it rewrite on user_pass2 that I created on meta data ! – Cameron A Mar 29 at 3:05
  • any work around for this? I used edit_user_profile_update , but it didnt work at all – Cameron A Mar 29 at 3:06
  • 1
    Yes - they both apply the same action hook. If you don't want that to happen, you can apply the is_admin() conditional tag as a check. I'll edit the code example above so that it reflects that. – butlerblog Mar 29 at 3:07
  • thanks very much, I was about to edit pluggable.php from core wordpress files as this article suggest to get the raw password and store it in database qnimate.com/find-your-wordpress-users-password-2 – Cameron A Mar 29 at 3:11
  • 1
    DO NOT under any circumstances edit core files directly. If you're making a change to a pluggable function, the way to do that is to load your function as a plugin - changing it in the actual core file is not what's intended (i.e. that's not "plugging" the function). – butlerblog Mar 29 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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