Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
Thank you very much! This is exactly what i was looking for. –  Davos Seaworth Jan 22 '12 at 17:26

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.

share|improve this answer
add_action('wp_logout', 'user_id');

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

use this in plugin main file

share|improve this answer
3  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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