WordPress Logout Redirect not working (Solved)
I know you all be so happy to see the title of the post. Yes there is a solution for adding a redirect after being logout.
Problem:
I was using
echo wp_logout_url( home_url() );
It seems so simple. When we look at the url it works properly with a parameter “redirect_to”. But whenever you try to use that it will always go to default wordpress login page. I researched and found out that domain mapping plugin is stopping it from redirecting. I tried fixing it in domain mapping itself but it was a waste of time .
Solution:
But then wordpress offers amazing power with hooks. Then i found out there is one hook where i could call a function after doing redirect.
add_filter('wp_logout','redirect_me');
This calls the function after deleting the cookie and signing out . So i wrote a function that will check if it has a redirect_to parameter in url then could redirect to destination else will redirect to where it came from. If you don’t want to redirect to HTTP_REFERER then you can always use
home_url()
function redirect_me(){ //if the user has rights of editor then return don't do anything if(current_user_can('editor')) return; //get the reffer or you may user home_url() if the "request_to" is not set $logout_redirect_url = $_SERVER['HTTP_REFERER']; if(!empty($_REQUEST['redirect_to'])) wp_safe_redirect($_REQUEST['redirect_to']); else wp_redirect($logout_redirect_url); exit(); }
Hope this solution helps .
Unquestionably consider that that you stated. Your favorite justification appeared to be on the net the easiest factor to bear in mind of. I say to you, I definitely get irked whilst other folks consider worries that they plainly don’t realize about. You managed to hit the nail upon the highest and defined out the entire thing with no need side effect , other people could take a signal. Will likely be back to get more. Thank you
Hi, Neat post. There is an issue with your site in internet explorer, might test this? IE nonetheless is the marketplace chief and a good element of people will omit your great writing because of this problem.
No this site was not tested on IE as i am using MAC, but thanks for noticing will defiantly look into it…
Very nice website, thanks for share this article with us