0

Lordy, I hope someone can help, I've run full circle and can't find a solution that works.

So we have posts that are locked, like this: https://inthebunch.co.za/2021/03/peter-sagan-delays-season-start-misses-strade-bianche/

The sign-in link points directly to the standard WordPress login (wp-login.php), all I want to do is send the reader back to the post they came from. I've tried so many codes, but each time the redirect is simply back to the home page.

The last code I used is:

// Function to redirect after login

add_filter('login_redirect', 'redirect_previous_page', 10, 1);

function redirect_previous_page( $redirect_to ){ global $user;

$request = $_SERVER["HTTP_REFERER"];

if ( in_array( $user->roles[0], array( 'administrator') ) ) {

    return admin_url();

} elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {

    return $request;
} 

return $redirect_to;

}

I've also tried this code:

function wpse_366921_hidden_login_field() {

echo "<input type='hidden' name='redirect_to' value='".$_SERVER['HTTP_REFERER']."'>";

} add_action( 'login_form', 'wpse_366921_hidden_login_field', 10, 0 );

1
  • I'd modify the 'simply sign in' link so that it's wp-login.php?redirect_to= current page. I don't know if there's an equivalent for register too though, but again that probably starts with passing the URL to the register page. – Rup Mar 5 at 10:29

Your Answer

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

Browse other questions tagged or ask your own question.