How can I get logged in user’s session data from admin-ajax?

I have hooked an ajax call for a logged-in user, and now I need to catch his data inside the receiving call (the code that receives the action call). How can I get the user’s ID reliably? This code is inside my plugin definition file and the code inside the function (error_log(“we’re in….kind of”);) is being called:

add_action( 'wp_ajax_create_team', 'create_team' );
 function create_team() {

    error_log("we're in....kind of");
 }
 }

2 Answers
2

wp_get_current_user will get you the WP_User object for the currently logged-in user:

add_action( 'wp_ajax_create_team', 'create_team' );
function create_team() {
    $current_user = wp_get_current_user();
    echo $current_user->ID;
    die;
}

Leave a Comment