0

What i need: To perform my own logic every time get_current_user is being called

Is it possible to use add_filter() to intervene in the get_current_user function logic?

1 Answer 1

1

From what I'm seeing in the github repo. get_current_user doesn't exist.
What you do have is wp_get_current_user, this function does this

return _wp_get_current_user();

So now I checked what _wp_get_current_user does.
It checks a global variable $global_user, if it's not empty, return an instance of it (didn't find any actions for that).
If empty it does a few checks and runs wp_set_current_user(), this function has one action

/**
 * Fires after the current user is set.
 *
 * @since 2.0.1
 */
do_action( 'set_current_user' );

So you can hook into it and use global $current_user;

_wp_get_current_user also has a filter, also at the end of the function block.

/**
 * Filters the current user.
 *
 * The default filters use this to determine the current user from the
 * request's cookies, if available.
 *
 * Returning a value of false will effectively short-circuit setting
 * the current user.
 *
 * @since 3.9.0
 *
 * @param int|false $user_id User ID if one has been determined, false otherwise.
 */
$user_id = apply_filters( 'determine_current_user', false );

I'm not sure how much help any of those events are but worth a shot.

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.