Ajax in wordpress custom plugin

If you have an engaged and active customer base for your website, you might wonder how you can provide a truly interactive and enriched web experience for them. Offering real-time interactivity can be a big draw for your audience.

ajax

jQuery(document).ready(function($) { 

//wrapper
  $(".pref").change(function() { //event
    var this2 = this; //use in callback
    $.post(my_ajax_obj.ajax_url, { //POST request
   _ajax_nonce: my_ajax_obj.nonce, //nonce
   action: "my_tag_count", //action
   title: this.value //data
   }, function(data) { //callback
        this2.nextSibling.remove(); //remove current title
        $(this2).after(data); //insert server response
      }
     );
} );
} );
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");
         }

        }

});

});

});

 

 

Leave a Reply