8

I need to perform some cleanup after a user logged out, so I added a wp_logout action hook. Problem is, wp_get_current_user() already returns null if called from within the wp_logout action hook. How do I get the logging out users userid inside a wp_logout action hook?

13

How about hooking 'clear_auth_cookie' with the cleaning you need to do? If you need even more depth, you can outright replace wp_clear_auth_cookie(), but that can get into issues where it will conflict with other plugins, so avoid that if possible.

  • Thank you very much! This is exactly what i was looking for. – Davos Seaworth Jan 22 '12 at 17:26
  • This worked perfectly. I have used this hook for a custom audit plugin, to record the logout event. Thank you. – julianm Feb 6 '15 at 16:44
-2

I know this is a little late.... but i ran into this problem my self. This is how i resolved the issue.

Add the following to your functions.php

function logout_redirect765(){
  $current_user = wp_get_current_user(); 
  wp_redirect( home_url().'/?userid='.$current_user->ID ); 
  exit; 
}
add_action('wp_logout','logout_redirect765');

This basically stores the userid in a url parameter when the user logs out.

Then simply get the parameter with the following:

$_GET['userid']

I hope this helps someone in the future who finds this post.

-3
add_action('wp_logout', 'user_id');

function user_id()
{
    wp_get_current_user()->data->ID;
}

use this in plugin main file

  • 4
    As the OP states 'wp_get_current_user() already returns null if called from within the wp_logout action hook' -- so this won't work. – marfarma Sep 24 '12 at 6:52

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.