Google

WordPress (always) redirect to homepage after logout.

Written on:July 27, 2011
Comments
Add One

This piece of code is trivial and very useful for development of custom sites and cms.

add to functions.php

add_filter('logout_url', 'projectivemotion_logout_home', 10, 2);

function projectivemotion_logout_home($logouturl, $redir)
{
$redir = get_option('siteurl');
return $logouturl . '&redirect_to=' . urlencode($redir);
}

Enjoy! Happy Coding!
- Amado Martinez / Projectivemotion Web Development

10 Comments add one

  1. How about changing the core file (wp-includes/general-template.php) to wp_logout_url( home_url() ) inside the wp_logout_url($redirect) function. In that way it always redirect to homepage whenever logout is triggered.

    • amado says:

      Hi. It’s not normally recommended to edit the wordpress include files directly because they can cause problems but more importantly because any time that you choose to update your installation there is a chance that your modification will be rewritten by any new code changes in the wordpress update. It’s better to use the functions.php file of your theme or create a custom plugin.

  2. Martin says:

    Hi, your piece of code was working good.
    HOW ABOUT WRONG LOGIN CREDENTIALS?
    Do you also have a code that redirect to a page and give a message?

    Hope to be in Mexico next month. To cold here in Europe!

  3. Thanks! When I add this code to functions.php in my localhost instance, it works perfectly. However, in production site, behavior remains unaltered. What could I be missing?

  4. Nat says:

    cheers for the easy hook/function. and i agree, easier to adjust the functions and leave as much as you can in one page because inevitably when we go back and try and find what we adjusted its a nightmare if we tried adjusting the actual code directly within the wp functions. been there done that!

  5. meher says:

    yes i am using this code. it works perfectly

  6. What a friggin lifesaver. Thanks!

  7. Francisco says:

    Than You very Much!

  8. qurat ul ain says:

    Thanks its really helpful :)

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>