0

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.

6
  • what is different between installations? is there cache for non-admins? what happens if you disable Woocommerce? Commented Jan 25, 2021 at 18:27
  • Can you edit your question to include the code you're debugging? Also have you considered using the newer REST API instead for your AJAX requests?
    – Tom J Nowell
    Commented Jan 25, 2021 at 18:50
  • @TomJNowell Post updated Commented Jan 25, 2021 at 19:07
  • @CelsoBessa Disabling WooCommerce does not resolve the issue. No caching. Lots of differences between the two installations, but the function of the plugin is very basic. Commented Jan 25, 2021 at 19:28
  • Have you checked the PHP error logs?
    – Tom J Nowell
    Commented Jan 25, 2021 at 20:09

1 Answer 1

0

Just another suggestion you could try. Rather than doing a separate AJAX call, could you just send it to your script via wp_localize_script? I'm not sure if that would have any effect on your particular bug, but it's worth a shot?

// Make data accessible to scripts
  wp_localize_script( 'my-script-handle', 'ajax_data', array( 
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    // ...other variables
    'user_email' => get_user_email(),
    // ...other variables
  ));
2
  • Thanks this will work! I suspect a plugin is responsible for the above bug, but I don't need full Ajax interaction here. This will totally work. Thank you! Commented Jan 26, 2021 at 13:53
  • Great! Glad I could help! :D
    – dkeeling
    Commented Jan 26, 2021 at 14:58

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.