Created
August 9, 2019 04:37
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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