I am not using any custom login plugins or any bespoke code. A few of my pages have got this bit of code in them at the very beginning.
<?php
if(!is_user_logged_in())
wp_redirect('/login/');
?>
So, this doesn’t allow the users to view the page when not logged in. I have these pages bearing this bit of code:
/wp-content/my-theme/my-account/
/wp-content/my-theme/my-account/world.php
/wp-content/my-theme/my-account/subscription.php
/wp-content/my-theme/my-dashboard.php
/wp-content/my-theme/my-files.php
Now, when a user goes to any of the above pages, without logging in, it redirects to the login page, and when the user logs in, it lands them to the my-account/
page.
I want to change the current scenario to make the user redirect to the referring page, where he came from. I tried the following things, which never worked.
Using a HTTP_REFERRER
In the login/
form, I placed this bit of code:
<input type="hidden" name="redirect" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
Hacking functions.php
In the functions.php
, I placed this bit of code:
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
wp_safe_redirect($location);
exit();
}
}
References:
- Redirect wordpress back to referring page after login
- WordPress Tip: Redirect to Previous Page After Login
I have also tried these and failed:
- Redirecting users to referrer page after logging in using custom login form
- Redirect user to original url after login?
Nothing was working out. I am happy to provide further details if needed. Thanks in advance. :)
Update
I have modified the code this way:
<?php
if(!is_user_logged_in())
wp_redirect('/login/?redirect_to=' . $_SERVER["REQUEST_URI"]);
?>
This renders the login page this way:
/login.php?redirect_to=/my-account/subscription.php
This would be enough for me to authenticate and redirect. But I need to find the bit where the real redirection happens and I want to redirect it using the redirect_to
parameter!
4 s
I’m not sure I understand your setup, but here are few ideas:
A) Display a login link with the redirect_to
parameter set:
You could add the following to your custom template pages:
if( ! is_user_logged_in() )
{
printf( '<a href="https://wordpress.stackexchange.com/questions/169704/%s">%s</a>',
wp_login_url( get_permalink() ),
__( 'You need to login to view this page!' )
);
}
This will generate a login link, for anonymous visitors, with the current page in the redirect_to
GET parameter.
B) Redirect to the wp-login.php
with the redirect_to
parameter set:
Notice that the call to wp_redirect()
must happen before the HTTP headers are sent.
We can call it within the template_redirect
hook:
add_action( 'template_redirect',
function()
{
if( ! is_user_logged_in()
&& is_page( array( 'member-page-1', 'member-page-2' ) )
)
{
wp_safe_redirect( wp_login_url( get_permalink() ) );
exit();
}
}
);
where we restrict the access to pages with the slugs member-page-1
and member-page-2
.
C) The native login form (in-line):
Another option would be to include the native login form, directly into the page content:
add_filter( 'the_content', function( $content ) {
if( ! is_user_logged_in()
&& is_page( array( 'member-page-1', 'member-page-2' ) )
)
$content = wp_login_form( array( 'echo' => 0 ) );
return $content;
}, PHP_INT_MAX );
where we restrict the access to pages with the slugs member-page-1
and member-page-2
.
Notice you would have to take care of the archive/index/search pages.
Update: I simplified it by using the wp_login_url()
function.