Skip to content

Instantly share code, notes, and snippets.

@aslamdoctor
Last active April 23, 2018 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslamdoctor/0a2e2bc1f77d8806e1a5c158ca8de725 to your computer and use it in GitHub Desktop.
Save aslamdoctor/0a2e2bc1f77d8806e1a5c158ca8de725 to your computer and use it in GitHub Desktop.
Ajax with Wordpress
// The HTML
wp_localize_script( 'amd_app_js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
// The PHP
add_action( 'wp_ajax_get_exhibitors', 'get_exhibitors_callback' );
add_action( 'wp_ajax_nopriv_get_exhibitors', 'get_exhibitors_callback' );
function get_exhibitors_callback() {
$alphabet = @$_POST['alphabet'];
$supplier_type = @$_POST['supplier_type'];
var_dump($_POST);
die();
}
// The Javascript
$.ajax({
type: 'POST',
url: ajax_object.ajax_url,
data: {
'alphabet' : 'A',
'supplier_type' : 'Worker',
'action': 'get_exhibitors'
}, success: function (result) {
alert(result);
reset_exhibitors();
},
error: function () {
alert("error");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment