Page 1 of 1

Go to previously viewed page after login

PostPosted: August 31st, 2010, 11:15 am
by jhg03
There is a login link on every page of my website. At any time, if a user decides to login is there a way to redirect them back to the page they were previously on (instead of redirecting them to the specified "members welcome page"?

Advice appreciated, thank you!

Re: Go to previously viewed page after login

PostPosted: August 31st, 2010, 7:03 pm
by Jason Caldwell
Yes, this would need to be built into your theme, or possibly into a Login Widget if you prefer. The s2Clean theme that comes with s2Member Pro handles this automatically.

Otherwise, here is how it's done.
Code: Select all
http://example.com/wp-login.php?redirect_to=http://example.com/a-particular-page

So after logging in, they'll be automatically redirected to the redirect_to value.

Re: Go to previously viewed page after login

PostPosted: August 31st, 2010, 8:49 pm
by jhg03
Thank you Jason for your reply and solution. It's close but not quite what I was looking for though. Let me see if I can explain this better.

If they were on the About page and logged in, I would want them to return to the About page.
If they were on the Services page and logged in, I would want them to return to the Services page.
(And I have over 50 pages and a login link is on all my posts, so assigning specific values to each of the links isn't an option ;) )

Instead of directing them to one specific page is there perhaps some other code I could include that would say, take them a couple steps back to where they were on my site previously. Just one string that I could use in all my login links?

Sorry for the confusion.

Re: Go to previously viewed page after login

PostPosted: August 31st, 2010, 11:41 pm
by Jason Caldwell
Hi there. Thanks for reporting back.

Here is how I would handle it:
Code: Select all
/wp-login.php?redirect_to=<?php echo rawurlencode($_SERVER["REQUEST_URI"]); ?>

So you will need some PHP for this. That PHP tag:
<?php echo rawurlencode($_SERVER["REQUEST_URI"]); ?>
will automatically insert a link to the current page. So clicking that link from anywhere on your site, will always redirect a visitor back to the page they were viewing before they logged in.

Re: Go to previously viewed page after login

PostPosted: September 9th, 2010, 9:59 am
by jhg03
This looks like exactly what I need, but I'm not sure how I should implement it in my code. (my php knowledge is very limited at the moment) I have added the php into the link but am sure I am doing it all wrong as it is not functioning properly.

Here's all my code: (login link in question is at the bottom)

Code: Select all
if (is_user_logged_in() && current_user_can("access_s2member_level4")){
    echo '<p class="login-links"><span class="login-greeting">Welcome back, ';
    echo S2MEMBER_CURRENT_USER_FIRST_NAME;
    echo '</span><br /><a href="
http://www.workplacecoachinstitute.com/cmd.php?cmd=cart">View Cart</a> <span class="login-spacing">|</span> <a href="http://workplacecoachinstitute.com/?s2member_profile=1">Profile</a> <span class="login-spacing">|</span> <a href="http://workplacecoachinstitute.com/wp-login.php?action=logout">Logout</a></p>';
} else {
    return '<p><a href="
http://www.workplacecoachinstitute.com/cmd.php?cmd=cart">View Cart</a> <span class="login-spacing">|</span> <a href="http://workplacecoachinstitute.com//wp-login.php?redirect_to=<?php echo rawurlencode($_SERVER["REQUEST_URI"]); ?>">Login</a></p>';
}


Thank you for your assistance.

Re: Go to previously viewed page after login

PostPosted: September 9th, 2010, 10:27 am
by Jason Caldwell
Hi there. Thanks for writing in.
Try this instead. You were really close, just a couple of things needed tweaking.
Code: Select all
<?php
if 
(is_user_logged_in () && current_user_can ("access_s2member_level4"))
    {
        echo '<p class="login-links">';
        echo '<span class="login-greeting">Welcome back, ' . S2MEMBER_CURRENT_USER_FIRST_NAME . '</span><br />';
        echo '<a href="http://www.workplacecoachinstitute.com/cmd.php?cmd=cart">View Cart</a> <span class="login-spacing">|</span> ';
        echo '<a href="http://workplacecoachinstitute.com/?s2member_profile=1">Profile</a> <span class="login-spacing">|</span> ';
        echo '<a href="http://workplacecoachinstitute.com/wp-login.php?action=logout">Logout</a>';
        echo '</p>';
    }
else
    {
        echo '<p>';
        echo '<a href="http://www.workplacecoachinstitute.com/cmd.php?cmd=cart">View Cart</a> <span class="login-spacing">|</span> ';
        echo '<a href="http://workplacecoachinstitute.com/wp-login.php?redirect_to=' . rawurlencode ($_SERVER["REQUEST_URI"]) . '">Login</a>';
        echo '</p>';
    }
?> 

Re: Go to previously viewed page after login

PostPosted: September 9th, 2010, 10:50 am
by jhg03
Perfect! Thank you very much!!!

Re: Go to previously viewed page after login

PostPosted: September 10th, 2010, 1:37 am
by Jason Caldwell
You're very welcome. Glad I could help.

Re: Go to previously viewed page after login

PostPosted: September 21st, 2010, 10:13 pm
by jhg03
Just discovered the redirect code works if someone is logging in with administrator privileges. However, it doesn't work if they have an s2Member level 4 role - just takes them back to the homepage of the website.

Any fix? - Thanks!

Re: Go to previously viewed page after login

PostPosted: September 26th, 2010, 8:43 am
by Guest
Is there a way how to stop s2member from redirecting users after login at all? I have a different login/redirect system - s2member hijacks this and forces users to go to frontpage even if there is no login welcome page setup in s2member config.

Re: Go to previously viewed page after login

PostPosted: September 26th, 2010, 10:36 pm
by drbyte
<a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">anything in here</a>

BUT

If you are using the Login Welcome Page to redirect members away from the admin backend then the code above or any code will not work. It will simple redirect them back to the welcome page

You can disable the redirection mode but that defeats the the whole purpose of protecting the backend of your site.

Sam

Re: Go to previously viewed page after login

PostPosted: September 28th, 2010, 10:17 am
by jhg03
Anyone?