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!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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="%s" rel="nofollow noreferrer noopener">%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.
Method 2
Usgin get_permalink(), as suggested in the accepted answer, will work only if you are in a post (of any type) but it won’t work, for example, if you are in a category archive. To make it work anywhere, the current URL is needed, not matter what type of content we are seeing.
To get the current URL in WordPress we can get the current request from the global $wp object and append it to the site url using add_query_arg(). We can use the result in the redirect_to parameter of wp_login_url() function:
wp_login_url( site_url( add_query_arg( array(), $wp->request ) ) );
The same approach can be used with wp_logout_url() if needed:
wp_logout_url( site_url( add_query_arg( array(), $wp->request ) ) );
Method 3
You can also use this code in your custom login page
if ( ! is_user_logged_in() ) { // Display WordPress login form:
$args = array(
'redirect' => esc_url($_SERVER['HTTP_REFERER']),
'form_id' => 'loginform-custom',
'label_username' => __( 'Email' ),
'label_password' => __( 'Password' ),
'label_log_in' => __( 'Log In' ),
'remember' => false
);
wp_login_form($args);
echo '<p><a href="' . wp_lostpassword_url($redirect) .'" rel="nofollow noreferrer noopener">Lost Password</a></p>';
}
This will redirect the user to referrer page, after login.
Method 4
I know it’s late, but I am posting the solution so that it can help someone in need at the moment.
This is what I am doing and it is working perfectly.
function redirect_user_tologin(){
global $post;
$post_slug = $post->post_name;
if(!is_user_logged_in()){
if($post_slug == 'my-account') { //can add multiple conditions
//create dynamic redirect page link
$redirect = urlencode(site_url().'/'.$post_slug);
//redirection
wp_safe_redirect(site_url().'/wp-login.php/?redirect_to=' .$redirect);
exit;
}
}
}
add_action('wp', 'redirect_to_login');
Explanation
If user is not logged in than wp_safe_redirect will take user to login page.
In order to redirect user after login, we will pass page link as value in redirect_to parameter so that it will take user to that particular page.
Login page url: site_url().'/wp-login.php/
Redirect page link: redirect_to=' .$redirect
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0