admin管理员组文章数量:1305383
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.
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.
Share Improve this question edited Jan 25, 2021 at 20:06 user1409059 asked Jan 25, 2021 at 18:18 user1409059user1409059 113 bronze badges 6- what is different between installations? is there cache for non-admins? what happens if you disable Woocommerce? – Celso Bessa 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 – user1409059 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. – user1409059 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
Reset to default 0Just 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
));
本文标签: Very weird bug Ajax for nonadmins
版权声明:本文标题:Very weird bug: Ajax for non-admins 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741804489a2398418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论