Created
December 8, 2013 09:03
-
-
Save ahmad24/7854908 to your computer and use it in GitHub Desktop.
wordpress : ajax skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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