I've been asked to implement a custom redirection in WordPress. It seemed quite simple at first but I have no idea how to get it right. So much that I can't even get started with it. This is how it should go:

  • The user logs in, and the database gets queried using the wp_login hook
  • If a certain condition is met, the user gets logged out using wp_logout() from the wp_login hook
  • This is where the problem comes in, the user should get redirected to a page dedicated to them, without breaking the normal logout function

So if the user logs out normally, they get redirected to the default page, however if the condition is met they get redirected to another page and still get logged out.

I've tried the wp_logout and wp_clear_auth_cookie hooks, but to no avail. I can't really show you any code, as I did not manage to come up with anything concrete.


 //in the login hook

 //below the login hook
 function logout(){
  • Maybe add some kind of if($condition) { header("Location: /page"); } to the wp_logout hook. The action to hook off is on line 603 of pluggable.php according to the codex link. add_action( $hook, $function_to_add, $priority, $accepted_args ); $function_to_add will be the one working out the condition to generate the header parameter? Thinking out loud! – Bysander May 12 '15 at 13:39
  • That problem is that I should be somehow passing the condition to the logout hook, and I have no idea how. I'll edit my post to show you what I've got. – Jack T May 12 '15 at 13:47
  • You shouldn't need to pass anything back just add an extra action to perform as part of it. It's not my expertise but I've put my suggestion below. – Bysander May 12 '15 at 13:58

The wp_logout() function is pluggable. You should be able to write your own version and "overload" the Core function:

function wp_logout() {

  $lid = get_current_user_id(); 


  do_action( 'wp_logout' );
    * Fires after a user is logged-out.
    * @since 1.5.0
  if (2 == $lid) {

  • Sorry for the silly question, but I don't really know WordPress, would this go into my theme's function.php or the pluggable.php file? – Jack T May 12 '15 at 14:25
  • Theme! ALWAYS theme to prevent overwriting during updates. Put it in your functions.php in your theme directory – Bysander May 12 '15 at 14:29
  • Thank you, and if I'm correct I don't need the add_action() call, right? – Jack T May 12 '15 at 14:31
  • Also if the condition is false, will this work as usual? – Jack T May 12 '15 at 14:37
  • If the condition doesn't match, it should work as normal. – s_ha_dum May 12 '15 at 14:37

The action to hook off is on line 603 of pluggable.php according to the codex link.

add_action( $hook, $function_to_add, $priority, $accepted_args ); is the format you should use so it would probably look something like:

 add_action( 'wp_logout' , 'logout_page' );

 function logout_page() {
   // Work out your conditions 

     if($condition1) {
       $page = *page*; // whatever page
     } else if ($condition2) {
       $page = *page*; // whatever page

   header("Location: /$page");

I haven't done much with the logout hook but unless the function has already output something (which it would seem unlikely at that stage) the header should take you to the right page.

  • The problem with this approach is that to work out my conditions I need the users ID as it's all coming from the database and if I use get_current_user() all I'll get will be null. – Jack T May 12 '15 at 14:00
  • If you set a high $priority your action should be performed before the user is logged out and you should be able to use get_current_user_id() ?? – Bysander May 12 '15 at 14:14
  • The function wp_destroy_current_session() uses get_current_user_id() so you should still be able to use it – Bysander May 12 '15 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.