Redirect to the Home Page After Logout in WordPress

By default WordPress sends users who log out to the login screen with a “You are now logged out” message. On a shop, a membership site or any site where customers log in, that WordPress login page is the last place they should land. This snippet redirects users to the home page after logout.

The snippet

<?php
/**
 * Send users to the home page after they log out of WordPress.
 */

// ===== Config =====
function dpc_logout_redirect_config(): array {
	return array(
		// true = a logout link that asks for a specific page (redirect_to) still goes there.
		'respect_redirect_to' => false,
	);
}
// ===== End of config =====

add_filter( 'logout_redirect', 'dpc_logout_to_home', 10, 2 );

function dpc_logout_to_home( $redirect_to, $requested_redirect_to = '' ) {
	$config = dpc_logout_redirect_config();

	if ( ! empty( $config['respect_redirect_to'] ) && '' !== (string) $requested_redirect_to ) {
		return $redirect_to;
	}

	return home_url( '/' );
}

How it works

  • wp-login.php?action=logout checks the nonce, logs the user out and then asks the logout_redirect filter where to go. The snippet answers with home_url( '/' ).
  • WordPress sends the user there with wp_safe_redirect(), so only your own site, or hosts you allow, can ever be the target.
  • The older version of this post called wp_redirect() and exit inside the wp_logout action. That stopped every callback that runs later on wp_logout, for example plugins that clear their own sessions or log the logout, and it also fired for logouts that should not redirect at all, such as a logout through the REST API or WP-CLI.
  • The function names start with dpc_ instead of the wpc_ prefix of the original source.

Add it with Scripts Organizer

In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor, edit the config and publish. Leave Action/Hook empty.

Logging out goes through wp-login.php, which is neither a front-end page nor wp-admin, so Conditions and Admin only would not load the code there. Everywhere does.

The same code also works unchanged in a small plugin or an mu-plugin.

Customise it

  • Keep logout links that ask for a page: set 'respect_redirect_to' => true. A link built with wp_logout_url( get_permalink() ) then returns to that page, and plain logout links still go home.
  • Another page than home: replace home_url( '/' ) with home_url( '/goodbye/' ) or get_permalink( 123 ).
  • Different targets per role: the filter’s third argument is the user who logged out. Add $user to the function, raise the accepted arguments in add_filter() to 3 and check in_array( 'customer', (array) $user->roles, true ).
  • The matching login side: Redirect wordpress back to referring page after login.

Gotchas

  • WooCommerce’s My account → Log out link goes through the same filter, so customers land on the home page instead of the My account login form.
  • Page caching: make sure your cache does not serve a logged-in version of the home page. Most caches skip users with a login cookie, and the cookie is gone after logout.
  • If another plugin also filters logout_redirect at a later priority, it wins. List everything hooked to the filter to find it.

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.