Action in Ajax request does not run for some user roles

I have an ajax POST request that looks like this:

var data = {
action: ‘like_prof’,
id: profID,
};

$.ajax({
method: “POST”,
url: ajax_url,
data: data,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( ‘X-WP-Nonce’, universityData.nonce );
},
success : function( response ) {
console.log(response);
},
error: function(response){
console.log(response);
}
});

The profID is the ID of the professor (a custom post type) that I am currently dealing with.

And here is what my functions.php contains:

function likeProfessor() {
$userLikesArray = get_post_meta($_POST[‘id’], ‘userLikesArray’, true);

if(!in_array (get_current_user_id() , $userLikesArray)){
array_push($userLikesArray, get_current_user_id());
}
else{
$userLikesArray = array_diff($userLikesArray, array(get_current_user_id()));
}

update_post_meta($_POST[‘id’], ‘userLikesArray’, $userLikesArray);
}
add_action(‘wp_ajax_like_prof’, ‘likeProfessor’);
add_action(‘wp_ajax_nopriv_like_prof’, ‘likeProfessor’);

‘userLikesArray’ is a metakey for professor. The function likeProfessor() in functions.php runs when I am logged in as an administrator but does not for other roles like subscriber. I have given subscriber the capability to edit, delete, and publish professor through the Members Plugin but it doesn’t fix the issue.
Any idea what the issue is and how to fix it?
Thanks in advance for any help!

Read more here:: Action in Ajax request does not run for some user roles

Leave a Reply

Your email address will not be published. Required fields are marked *