Arrow

logout redirect to last visited page. | Peter's Login Redirect | Forum

Back to the login redirect plugin page

Please consider registering
guest

Log In

Lost password?
Advanced Search

— Forum Scope —

  

— Match —

   

— Forum Options —

   

Minimum search word length is 4 characters - maximum search word length is 84 characters

Topic RSS
logout redirect to last visited page.
February 25, 2012
11:55 am
Dave Pitman
Guest

To use a function instead of a plugin, I found this code attributed to Peter.

function redirect_logout_home_page() {
wp_redirect(get_option('home'));
die();
}

add_action('wp_logout', 'redirect_logout_home_page');

This works fine to redirect to the home page.

Can you share how to edit this to redirect to the last page the visitor was on when they chose to log-out.

Thank You!

February 25, 2012
12:31 pm
Peter
Admin
Forum Posts: 841
Offline

I usually recommend seeing this comment for ideas, as there are a few ways to handle that on login and logout:

http://www.theblog.ca/wplogin-.....ment-11089

February 25, 2012
1:41 pm
Dave Pitman
Guest

Hmmm. It seems that it is not as simple to have a global redirect to the previous visited page as it is to an absolute url.

I played around with using "$_SERVER['HTTP_REFERER']" as the value, but, no joy.

The WP-Codex gives the example of

<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout">Logout</a>`

for this purpose, but no examples of how to use this within functions.php rather than on a page template.

Can this or something else be adapted to apply this global logout redirect?

Thanks Peter, I apologize for what must be basic 'crap' for you! Smile

February 25, 2012
10:04 pm
Peter
Admin
Forum Posts: 841
Offline

Hi Dave,

The example you posted might be your best bet -- not putting the logic in functions.php but rather editing your theme template and making the logout link dynamic.

February 25, 2012
11:53 pm
Dave Pitman
Guest

I cobbled together a slightly more useful function for using in page templates.

<?php wp_loginout(get_permalink()); ?>

Thanks for giving it some thought and recommendation.

March 25, 2012
9:59 am
stefano
Guest

Hi,
I would like users be redirected to the last visited page after they login or logout. Shall I use this funtion ?
And in which php file shall I paste it?

Regards

Stefano

July 25, 2012
10:14 am
filip
Guest

Stefano,
iI exactly needed that.

In simplemodal-login.php line 370 place:

<input type="hidden" name="referer" value="'. $_SERVER["REQUEST_URI"] . '" />

and to line 455 place:

if ($redirect_to == "referer")
$redirect_to = $_POST['referer'];

In admin interface in the URL redirection type "referer".

Peter, wouldnt it be a nice improvement of your cool plugin?

Cheers,
Filip

July 25, 2012
10:22 am
Peter
Admin
Forum Posts: 841
Offline

Thanks a lot for sharing, Filip! I've left this functionality out of the core on purpose because of the variety of ways people customize their login forms and because you can extend this plugin with the code that you've shared:

http://wordpress.org/extend/pl.....her_notes/