6

I want to use another link for logout because I have /wp-admin/* protected with htpassword. Is there a way to do this? Something like creating a custom page like site.com/logout then use it as my new logout link?

Thanks!

6 Answers 6

4

You can filter 'logout_url' and return a custom value if you are in the admin area:

add_filter( 'logout_url', 'wpse_58453_logout_url' );
function wpse_58453_logout_url( $default ) 
{
    // set your URL here
    return is_admin() ? 'http://example.com/custom' : $default;
}
9
  • But why do i get 404? I tried mysite.com/logout but i get 404 do i need to add some code on the end url?
    – tiltdown
    Commented Jul 14, 2012 at 7:12
  • 1
    Well, the target should exist somewhere. :) You could create a custom endpoint (example).
    – fuxia
    Commented Jul 14, 2012 at 7:15
  • What will do with the endpoint? I created a page called 'logout' and it just redirects it there what do i need to add on the logout page? Thank you for your reply.
    – tiltdown
    Commented Jul 14, 2012 at 7:27
  • I am not sure if I still follow you … maybe that’s a separate question?
    – fuxia
    Commented Jul 14, 2012 at 8:21
  • No same question. Here's the thing i followed your function added it on my custom function file then made it to "mysite.com/logout" the logout is a WordPress page. When i do <?php echo wp_logout_url( ); ?> it gives me a the right logout url which is mysite.com/logout but when i click on it it will just redirect me to the /logout WordPress page. Do i need to setup any code on my /logout page on wordpress?
    – tiltdown
    Commented Jul 14, 2012 at 8:27
2

I think you should add rewrite rule in your htaccess file like this.

RewriteRule ^logout/(.*) /wp-login.php?action=logout&_wpnonce=$1 [QSA,L]
2

This worked for me redirecting to the home page:

add_action('wp_logout','unlog');

function unlog(){
  wp_redirect( site_url() );
  exit();
}
1
  • Thank you, this works. The accepted answer just did a redirect without a logout. Commented May 13, 2019 at 14:53
0

I found the solution here: https://www.kmbytes.com/wordpress-logout-redirect-filter/

And it is working great!

add_filter( 'logout_url', 'my_logout_url' );
function my_logout_url( $url ) {
    $redirect = home_url();
    return $url.'&redirect_to='.$redirect;
}

Cheers!

0
    function wp_loginout( $redirect = '', $echo = true ) {
    if ( ! is_user_logged_in() ) {
        $link = '<a href="' . esc_url( wp_login_url( $redirect ) ) . '">' . __( 'Log in' ) . '</a>';
    } else {
        $link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __( 'Log out' ) . '</a>';
    }
 
    if ( $echo ) {
        /**
         * Filters the HTML output for the Log In/Log Out link.
         *
         * @since 1.5.0
         *
         * @param string $link The HTML link content.
         */
        echo apply_filters( 'loginout', $link );
    } else {
        /** This filter is documented in wp-includes/general-template.php */
        return apply_filters( 'loginout', $link );
    }
}

Displays a link, which allows users to navigate to the Log In page to log in or log out depending on whether they are currently logged in.

2
0

A very interesting solution is to create a Page "Logout" and then create the template of it in main folder of your theme by file page-{your-logout-slug}.php. And here is the simplest code of this page.

    if ( is_user_logged_in() ) : 
        wp_logout();
        wp_redirect(site_url());
    else : 
        wp_redirect(site_url());
    endif;

Add argument to site_url() of any slug or leave empty to be logged out to your home page. It's good because by using any php code you can do many things before you will be logged out.

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.