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?

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'];
2

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>
  • I need it for the function-file. Not as a link, but thanks for the try :) – Demilio Mar 1 '12 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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