admin管理员组

文章数量:1435506

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

Share Improve this question edited Mar 29, 2019 at 7:54 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Mar 28, 2019 at 22:24 zEn feeLozEn feeLo 2073 silver badges18 bronze badges 7
  • I think you want to use the personal_options_update action instead of (or in addition to) profile_update. – butlerblog Commented Mar 29, 2019 at 2:34
  • the profile_update works right now, the problem is I cant access the $_POST['password'] or $_POST['pass1'], it returns nothing – zEn feeLo Commented Mar 29, 2019 at 2:36
  • I updated the question – zEn feeLo Commented Mar 29, 2019 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 Commented Mar 29, 2019 at 2:40
  • Im exactly updating password via woocommerce account detail page – zEn feeLo Commented Mar 29, 2019 at 2:44
 |  Show 2 more comments

1 Answer 1

Reset to default 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).

本文标签: