I am working on a site where one specific post type is hidden behind a login. For this I used wp_login_form to replace the_content. This is working fine, but I also want to add a Lost Password link below the login form, and I want the user to get redirected back to the post after password reset. For this, I am attempting the following:
First, to generate the password reset link I am passing the post permalink to the lost password page in query string "redirect":
$content .= '<a href="' . wp_lostpassword_url() . '&redirect=' . get_permalink() . '">Reset Password</a></div></div>';
Then, to filter the message, I am trying to get the referer url from $_POST and retrieve that redirect parameter:
add_filter( 'retrieve_password_message', 'collab_retrieve_password_message', 10, 4 );
function collab_retrieve_password_message( $message, $key, $user_login, $user_data ) {
$site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
$message = __( 'Someone has requested a password reset for the following account:' ) . "\r\n\r\n";
$message .= sprintf( __( 'Site Name: %s' ), $site_name ) . "\r\n\r\n";
$message .= sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n";
$message .= __( 'If this was a mistake, you can simply ignore this email and your password will remain unchanged.' ) . "\r\n\r\n";
$message .= __( 'To reset your password, visit the following address:' ) . "\r\n\r\n";
$parts = parse_url($_POST['referer']);
parse_str($parts['query'], $query);
$redirect = $query['redirect'];
$message .= $redirect . "\r\n\r\n";
$message .= '<' . home_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ) . "&redirect_to=" . $redirect) . ">\r\n";
return $message;
}
You can see I output the redirect in both the body and the link for testing, but both are coming back blank.
Does anyone have any other ideas on how I can pass the original post url as a redirect parameter on the password reset link? Thanks in advance!