WordPress (always) redirect to homepage after logout.

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

  • 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

      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.

  • Hi, your piece of code was working good.
    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!

  • 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?

  • Nat

    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!

  • meher

    yes i am using this code. it works perfectly

  • What a friggin lifesaver. Thanks!

  • Than You very Much!

  • qurat ul ain

    Thanks its really helpful 🙂

  • Bis

    Couldn’t agree more – “very useful for development”! 🙂

  • Roshan

    I added this code to the functions.php file of my theme, at the end of the file, but it doesn’t work? What am I doing wrong?