1

I have looked at a couple of different ways to check when a user, of a particular role, has logged out. The call is made within a plugin.

In the main loop I have the following:

add_action( 'wp_logout', mmd_JudgeLogoutCheck, 10,0);

function mmd_JudgeLogoutCheck()
{
$current_user  = wp_get_current_user(); 
if ( in_array( 'judge', (array) $current_user->roles ) )  
  {  
    mmd_StoreJudgeStatus($current_user->user_email, JUDGE_LOGGED_OUT, 0);  
  } 
}

Each time the call to the wp_get_current_user returns a blank. That email is key to my functionality. I also tried


add_action( 'wp_logout', function() {
    $user = wp_get_current_user();
    mmd_JudgeLogoutCheck($user);
    // ...
}, 10, 0);

function mmd_JudgeLogoutCheck($current_user  )
{
if ( in_array( 'judge', (array) $current_user->roles ) )  
  {  
    mmd_StoreJudgeStatus($current_user->user_email, JUDGE_LOGGED_OUT, 0);  
  } 
}


Same results. Calls that happen earlier do not appear to have user information. Any assistance with how to get which particular user is logging out, would be helpful.

2 Answers 2

2

wp_get_current_user() will never work in this hook, because, wp_logout action fires after user is logged out: session destroyed, cookies cleared and current user is set to 0.

But wp_logout action recieves $user_id. I will give you a working example, because I do not familiar with your custom functions.

//pass $user_id as argument
function mmd_JudgeLogoutCheck($user_id)
{
    $user  = get_userdata($user_id); 
    if ( $user instanceof WP_User  )  
    {  
        //user email available here
        die($user->user_email);  
    } 
}

//here last argument should be one
add_action( 'wp_logout', 'mmd_JudgeLogoutCheck', 10,1);
6
  • Interesting. I can look at that as well. I found the clear_auth_cookie cookie call, carried the information. Thanks Commented May 6, 2021 at 18:50
  • 1
    Like wp_logout action more, because on this stage everything is cleared on 100% = user is logged out.
    – anton
    Commented May 6, 2021 at 18:52
  • For grins, I tried it. YUP, that worked too and does not interfere with woo commerce as well. Great suggestion. Commented May 6, 2021 at 18:54
  • Accept my answer or your (if you can) as right answer, please. It will be removed from unanswered section, so we can help other people more efficiently. thanks =)
    – anton
    Commented May 6, 2021 at 19:06
  • I already accepted your answer Commented May 6, 2021 at 19:08
-1

Would you not know it, the minute I wrote the question, I thought of another idea and it worked. Here is the solution:

In the plugin main, add the following INSTEAD OF :

add_action( 'clear_auth_cookie', mmd_JudgeLogoutCheck, 10,0); <<<< ADD THIS INSTEAD
///add_action( 'wp_logout', mmd_JudgeLogoutCheck, 10,0);   <<<<< REMOVE THIS

function mmd_JudgeLogoutCheck()
{
$current_user  = wp_get_current_user(); 
if ( in_array( 'judge', (array) $current_user->roles ) )  
  {  
    mmd_StoreJudgeStatus($current_user->user_email, JUDGE_LOGGED_OUT, 0);  
  } 
}

Appears that clearing the authorization cookie, carries the user information with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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