Skip to content

Instantly share code, notes, and snippets.

@SalmanRavoof
Created August 9, 2019 04:37
Show Gist options
  • Save SalmanRavoof/6f8ad5c3e23716b4f5872649857e622f to your computer and use it in GitHub Desktop.
Save SalmanRavoof/6f8ad5c3e23716b4f5872649857e622f to your computer and use it in GitHub Desktop.
JavaScript file to add AJAX functionality to your plugin. The 'myAjax.ajaxurl' value is the admin-ajax.php file for your website.
jQuery(document).ready( function() {
jQuery(".user_like").click( function(e) {
e.preventDefault();
post_id = jQuery(this).attr("data-post_id");
nonce = jQuery(this).attr("data-nonce");
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "my_user_like", post_id : post_id, nonce: nonce},
success: function(response) {
if(response.type == "success") {
jQuery("#like_counter").html(response.like_count);
}
else {
alert("Your like could not be added");
}
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment