1

clear_auth_cookie is what I'd need in order to catch the actual logout action, but I can't get it to return anything.

https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/pluggable.php#L940

As per the Codex, it doesn't give any information.

Ideally I'd like to do:

add_action( 'clear_auth_cookie', 'return_user_data_on_logout');

function return_user_data_on_logout( $user ) {
    $id = $user->ID; //Assuming it returns a WP_User object.
    //Do some logic here, mostly to check if the user if is of certain type     / 
    has certain meta attached to them.
}

Any ideas?

2 Answers 2

1

That action doesn't pass that data:

function return_user_data_on_logout( $user ) {

Here, $user will alway be undefined. Additionally, you need to tell add_action how many parameters the function takes.

But..

do_action( 'clear_auth_cookie' );

No information is passed to begin with, that's not how this particular event/action works.

So how do we get the current user being logged out? The answer is we remove the words "being logged out" from that question giving us a much easier question that is far more searchable:

How do we get the current user?

$user = wp_get_current_user();

It's possible that this hook may be too early, and additionally it may not be the best hook to use.

For example, wp_logout is a much better hook to use as it says on the tin what it does

So:

add_action( 'wp_logout', function() {
    $user = wp_get_current_user();
    // ...
});
6
  • Works well, it's weird, I tried it before but I must've gotten lost in something that it didn't work.
    – coolpasta
    May 10, 2018 at 22:46
  • When exactly should this fire in order to register? It seems that if I register it late, it just doesn't work.
    – coolpasta
    May 10, 2018 at 22:58
  • That's a good question, and one you should ask, a comment won't do it justice
    – Tom J Nowell
    May 11, 2018 at 1:00
  • I tried this within a plugin. The call definitely happened, but the call to the get current user returns empty. May 6, 2021 at 17:40
  • @DebbieKurth without more information it's not possible to diagnose what happened. You should ask a question and include the code you tried and as much detail as possible. My answer is intended only for the original question.
    – Tom J Nowell
    May 6, 2021 at 18:55
0

I found a solution and another individual gave me a second and better solution. That dialogue can be found here: Getting User email on logout. wp_logout

Here the two solutions:

SOLUTION 1:

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);  
  } 
}

SOLUTION 2: BETTER. As it does not interfere with woocommerce logout functionality.

add_action( 'wp_logout', 'mmd_JudgeLogoutCheck', 10,1);  <<< NOTE PASSING 1 PARAMETER
function mmd_JudgeLogoutCheck($user_id)
{
$current_user  = get_userdata($user_id);

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

2
  • Your function name is missing quotes, this code will generate PHP Fatal errors
    – Tom J Nowell
    May 6, 2021 at 19:04
  • Well it is not. And if you look at how I defined the CONSTANT it passes the correct value. It is in use as we speak. No errors. May 6, 2021 at 19:05

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.