Skip to content

Instantly share code, notes, and snippets.

@ahmad24
Created December 8, 2013 09:03
Show Gist options
  • Save ahmad24/7854908 to your computer and use it in GitHub Desktop.
Save ahmad24/7854908 to your computer and use it in GitHub Desktop.
wordpress : ajax skeleton
<SCRIPT TYPE="text/javascript">
var data = {
action: 'boj_myplugin_do_ajax_request',
some_var: 'some value',
other_var: 'other value'
};
jQuery.post( 'http://example.com/wp-admin/admin-ajax.php', data, function( resp ) {
/*
1. process response object 'resp'
2. update part of page
*/
});
</SCRIPT>
<?php
// wp_ajax_$action hooks functions if the user is logged in.
// wp_ajax_nopriv_$action hooks functions if the user is not logged in and has no privilege.
add_action( 'wp_ajax_boj_myplugin_do_ajax_request', 'boj_myplugin_process_ajax' );
// process Ajax data and send response
function boj_myplugin_process_ajax() {
// check authority and permissions: current_user_can()
// check intention: wp_verify_nonce()
// process data sent by the Ajax request
// echo data response that the Ajax function callback will process
die();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment