Redirect to base URL on logout in WordPress

After a user signs out, the default behaviour in WordPress is to send the user back to /wp-login.php

However, it’s usually more desired to send the user back to the base / root URL for their site. Here’s a small plugin that accomplishes exactly that:

<?php
/*
Plugin Name: Redirect to Base URL on logout
Plugin URI: https://snippets.khromov.se/redirect-to-base-url-on-logout-in-wordpress
Description: Redirect to Base URL on logout
Version: 2015.01.11
Author: khromov
Author URI: https://snippets.khromov.se
License: GPL2
*/
add_action('wp_logout', function()
{
    wp_redirect(get_home_url());
    exit;
});

If you prefer, you can use the code block (without plugin header) directly in your themes functions.php file.

4 thoughts on “Redirect to base URL on logout in WordPress

  1. Chordian

    This redirected me back to the wordpress subfolder (which in turn showed me the 404 page). To make it go back to the root I had to change:

    wp_redirect(get_site_url());

    To:

    wp_redirect(home_url());

    Reply
    1. Stanislav Khromov Post author

      Hey Chordian,

      Do you happen to work on a domain-mapped multisite, or non-standard install? (manually moving the index.php file)

      In general, it looks like home_url() is the best option, so I have updated the snippet accordingly.

      Relevant discussion:
      http://wordpress.stackexchange.com/questions/20294/whats-the-difference-between-home-url-and-site-url
      http://www.skywordpress.com/site_url-vs-home_url/
      https://vip.wordpress.com/documentation/vip-development-tips-tricks/home_url-vs-site_url/

      Reply
      1. Chordian

        It was when testing locally in WampServer 2.5 with virtual hosts (i.e. my test site there is currently a simple one just like “localhost”). Maybe it works correctly online but I haven’t tested that as my WordPress theme is still a work in progress.

        Reply
        1. Stanislav Khromov Post author

          Looks like it depends on your configuration and running plugins.
          get_home_url() should hopefully work regardless.

          Reply

Leave a Reply

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

Markdown is allowed in comments.