0

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

  • This will not work. You have put your ; at the wrong spot. You should not end the anonymous function with a semicolon, as it will end the add_action() function statement before it should. Put your semicolon after the add_action parenthesis closed. – dboris Sep 4 at 12:17
  • Good catch @dboris. I wrote that code outside of WordPress and then copy / pasted completely missing the fact that the semicolon would end the line, duh. Thank you! – Ted Stresen-Reuter Sep 4 at 12:45
0

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

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.