how to use ajax in wordpress

There are few simple steps to use ajax in wordpress:
– ajax calling template/page file element (button / link)
– ajax calling and response handler script in js/jqery
– wordpress hooks defined to handle ajax functions
– php function file to define functions to operate.

Files used in wordpress:
– any template file or page or post and for js ajax calling

jQuery(document).ready(function(){
jQuery('#wp_agent_sync').click(function(){
jQuery.ajax({
type : "post",
dataType : "json",
url : '',
data : {action: "podiocall",method:'agent'},
success: function(response) {
alert(response.msg);
}
}) ;
});

– function.php for defining ajax hoooks
example :
add_action("wp_ajax_podiocall", "podioListingCall");
– function.php for php function definition or any included library.
function podioListingCall()
{

$method = in_array($_POST['method'],array('listing','agent')) ? $_POST['method'] = false;
if($method==false) echo json_encode(array('response'=>'fail','msg'=>'Unknown method called!'));
else
{
echo json_encode(array('response'=>'success','msg'=>'podio called,Thanks'));
}
die();
}

Thanks for reading!

contact us