I am seriously stumped. For some reason, a perfectly good and simple AJAX function (which exposes the user's email address to the frontend) is working for non-admins in one installation but not another. I've verified the code is accurate, both in PHP and Ajax. I'm registering, localizing, and enqueueing the script correctly. It works for admin but not for non-admins.
I've gone through and disabled all plugins to determine some kind of plugin conflict. I've rolled back to the 2019 theme. All to no avail.
I'm using a few plugins which may have affected the subscriber capabilities, but I can't determine which capability specifically may have been affected.
Any advice at all here?
EDIT: Here is the PHP:
add_action( "wp_ajax_get_user_email", "get_user_email" );
add_action( "wp_ajax_nopriv_get_user_email", "get_user_email" );
function get_user_email() {
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$email_address = $user_info->user_email;
echo $email_address;
wp_die();
}
Here is the JS:
jQuery( document ).ready( function( $ ) {
var data = {
"action": "get_user_email",
};
jQuery.post(ajax_object.ajax_url, data, function(response) {
console.log("Email: " + response);
});
});
I have considered using the REST API as an alternative. I've just been able to fix this so quickly in the past, very frustrating.