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?

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();
    // ...
});
  • 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 '18 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 '18 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 '18 at 1:00

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.