Я пытаюсь обновить мета пользователя с помощью update_user_meta
, но, похоже, эта функция работает только для пользователей с правами администратора, а не для других пользователей.
function update_user_info()
{
// First check the nonce, if it fails the function will break
check_ajax_referer('user-update-nonce', 'security');
$currentUserId = get_current_user_id();
$num = update_user_meta($currentUserId, 'date_of_birth', esc_attr($_POST['dob']));
echo json_encode(array('updated'=>true, 'message'=>__('updated.')));
die();
}
function please_login() {
echo json_encode(array('updated'=>false, 'message'=>__('You are not logged in')));
die();
}
add_action('wp_ajax_updateuserinfo', 'update_user_info');
add_action('wp_ajax_nopriv_updateuserinfo', 'please_login');
Код AJAX
document
.getElementById("personalInfoUpdateForm")
.addEventListener("submit", this.editUser);
editUser(e) {
const updPerBtn = document.getElementById("updPerBtn");
function getGender() {
if (document.getElementById("male").checked == true) {
return "male";
} else {
return "female";
}
}
$.ajax({
url: studymoreData.ajax_url,
type: "POST",
dataType: "json",
data: {
action: "updateuserinfo",
dob: document.getElementById("dob").value,
security: studymoreData.user_update_nonce,
},
success: (response) => {
if (response.updated == true) {
console.log(response);
} else {
console.log(response);
}
},
error: (response) => {
console.log("Something went wrong");
console.log(response);
},
});
e.preventDefault();
}
Всякий раз, когда я запускаю код для пользователя с правами администратора, я получаю «обновленный» результат.
Но когда я запускаю код для пользователей без прав администратора, я получаю результат «Что-то пошло не так» .
Пожалуйста, помогите.