Logout redirect to current page – function

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?

Related posts

Leave a Reply

4 comments

  1. 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. 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.