Wordpress Ajax :: 2018-04-07 14:10:58

// working wordpress ajax
(function( $ ) {

var ajaxscript = { ajax_url : ‘/wp-admin/admin-ajax.php’ }

doSomething = function(){

$.ajax({
url : ajaxscript.ajax_url,
data : {
action : ‘my_callback’,
id : 1
},
method : ‘POST’, //Post method
success : function( response ){ console.log(response) },
error : function(error){ console.log(error) } ,
cache: false
});
return false; //disable event (onclick)

}

})(jQuery)
// working wordpress ajax

/** PHP (put this in functions.php)
add_action( ‘wp_ajax_my_callback’, ‘my_callback’ );
add_action( ‘wp_ajax_nopriv_my_callback’, ‘my_callback’ );
function my_callback(){
echo json_encode(‘{ “post_action”:’.$_POST[‘action’].’,”post_id”:’.$_POST[‘id’].’}’);
die();
}
**/

« »