Archive

Posts Tagged ‘logout error’

WordPress Logout Redirect not working (Solved)

May 17, 2012 4 comments

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 .

%d bloggers like this: