By default, in WordPress, whenever a user logs out, it will get redirected to the login page of your website. This may not be the best user experience, we may went to redirect them to a different page instead.
We can easily redirect users after logout to the homepage of WordPress site by adding following code snippet to your website:
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( home_url() );
exit();
}
Code language: PHP (php)
Alternatively, we can also set a custom internal or external URL by using this snippet instead:
add_action('wp_logout','auto_redirect_external_after_logout');
function auto_redirect_external_after_logout(){
wp_redirect( 'https://www.trickspanda.com' );
exit();
}
Code language: PHP (php)
Replace https://www.trickspanda.com with the URL where you want users to be redirected after logging out.
Thanks for the code. How can you remove or hide pre logout message: You are attempting to log out of websitename
Do you really want to
Thanks
I think there is a filter to change the logout url…
https://codex.wordpress.org/Plugin_API/Filter_Reference/logout_url
https://codex.wordpress.org/Plugin_API/Filter_Reference/logout_url
Yes but this article is not about changing the logout url, which is automatically generated by WordPress, but to redirect users to certain page after they logged out
Awesome. Thank you.
Just a couple of things :
– If you redirect to your own homepage please use “wp_safe_redirect()” instead.
– Exit statement does not require ()