Auto login after reset password

I found this code around and I'm testing it but it doesn't work.How can I solve it?add_action( 'validate_

I found this code around and I'm testing it but it doesn't work.

How can I solve it?

add_action( 'validate_password_reset', 'rsm_redirect_after_rest', 10, 2 );
function rsm_redirect_after_rest($errors, $user) {
    if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
        reset_password( $user, $_POST['pass1'] );
        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
        wp_set_current_user( $user->ID );
        wp_set_auth_cookie( $user->ID );
        do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
        wp_redirect( home_url() );
        exit;
    }
}

This is the error I get:

Warning: Cookie names must not be empty

Thanks to those who will help me.

RESOLVED:

I used the following code as recommended by you and now it works perfectly:

add_action( 'validate_password_reset', 'rsm_redirect_after_rest', 10, 2 );
function rsm_redirect_after_rest($errors, $user) {
    global $rp_cookie, $rp_path;
    if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
        reset_password( $user, $_POST['pass1'] );
        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
        wp_set_current_user( $user->ID );
        wp_set_auth_cookie( $user->ID );
        do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
        wp_redirect( home_url() );
        exit;
    }
}

I found this code around and I'm testing it but it doesn't work.

How can I solve it?

add_action( 'validate_password_reset', 'rsm_redirect_after_rest', 10, 2 );
function rsm_redirect_after_rest($errors, $user) {
    if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
        reset_password( $user, $_POST['pass1'] );
        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
        wp_set_current_user( $user->ID );
        wp_set_auth_cookie( $user->ID );
        do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
        wp_redirect( home_url() );
        exit;
    }
}

This is the error I get:

Warning: Cookie names must not be empty

Thanks to those who will help me.

RESOLVED:

I used the following code as recommended by you and now it works perfectly:

add_action( 'validate_password_reset', 'rsm_redirect_after_rest', 10, 2 );
function rsm_redirect_after_rest($errors, $user) {
    global $rp_cookie, $rp_path;
    if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
        reset_password( $user, $_POST['pass1'] );
        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
        wp_set_current_user( $user->ID );
        wp_set_auth_cookie( $user->ID );
        do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
        wp_redirect( home_url() );
        exit;
    }
}
Share Improve this question edited Sep 4, 2019 at 14:23 Matteo Feduzi asked Sep 4, 2019 at 8:55 Matteo FeduziMatteo Feduzi 291 silver badge9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

$rp_cookie and $rp_path are inside a function but are not initialized inside the function so they are out of scope and thus, empty. You can try calling global $rp_cookie, $rp_path; at the start of your function but I don't know if those variables have been set at the time your function is running and thus they might still be empty.

Another, more "modern" approach is to add the variable to the function scope by modifying the function signature as such:

add_action( 'validate_password_reset',
function rsm_redirect_after_rest($errors, $user) use ($rp_cookie, $rp_path) {
    if ( ( ! $errors->get_error_code() ) && isset( $_POST['pass1'] ) && !empty( $_POST['pass1'] ) ) {
        reset_password( $user, $_POST['pass1'] );
        setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
        wp_set_current_user( $user->ID );
        wp_set_auth_cookie( $user->ID );
        do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
        wp_redirect( home_url() );
        exit;
    }
}, 10, 2 )

Note the use ($rp_cookie, $rp_path) following the anonymous function, but again, if those variable have not been set prior to running this function, they will still be empty. HTH.

There is nothing wrong with your code, except that you are using two variables which are not set in your code, and you probably don't even need them. Please remove this line entirely and try again:

setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745185841a4615635.html

相关推荐

  • Auto login after reset password

    I found this code around and I'm testing it but it doesn't work.How can I solve it?add_action( 'validate_

    12小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信