5

I'm looking for a function that can redirect users when they logout to the current page. I'm looking for a code I can use in functions.php?

4 Answers 4

8

Using this code in the functions.php does the trick:

function wpse_44020_logout_redirect( $logouturl, $redir )
{
    return $logouturl . '&redirect_to=' . get_permalink();
}
add_filter( 'logout_url', 'wpse_44020_logout_redirect', 10, 2 );

Note - The above code works only on non-admin pages. To make this work on any page you should replace:

return $logouturl . '&redirect_to=' . get_permalink();

With:

return $logouturl . '&redirect_to=http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
3

Have a look here - http://codex.wordpress.org/Function_Reference/wp_logout_url

Easy as adding this:

<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout">Logout</a>
1
  • I need it for the function-file. Not as a link, but thanks for the try :)
    – Demilio
    Mar 1, 2012 at 13:05
0

It's really easy:

global $post;
$postid = $post->ID;

$log_out_me = wp_logout_url( get_permalink($postid) );

<a href="'<?php echo $log_out_me; ?>">Logout</a>

Now get_permalink() is out of the loop.

-3
add_filter('logout_url', create_function(false, "return '" .  wp_logout_url(get_option("home")) . "';"));  

Or change home URL to current page URL.

2
  • 3
    Please add some context to this answer - how does it add substance to what amit said a few months back? Apr 11, 2013 at 6:01
  • 1
    This callback would call itself in an infinite recursion. Please test your code before you post it.
    – fuxia
    Apr 11, 2013 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.