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
// embed the javascript file that makes the AJAX request | |
// don't forget to specify 'json2' as the dependency | |
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery', 'json2' ) ); |
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
jQuery('#myForm1').ajaxForm({ | |
data: { | |
// additional data to be included along with the form fields | |
}, | |
dataType: 'json', | |
beforeSubmit: function(formData, jqForm, options) { | |
// optionally process data before submitting the form via AJAX | |
}, | |
success : function(responseText, statusText, xhr, $form) { | |
// code that's executed when the request is processed successfully |
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
// if both logged in and not logged in users can send this AJAX request, | |
// add both of these actions, otherwise add only the appropriate one | |
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' ); | |
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' ); | |
function myajax_submit() { | |
$nonce = $_POST['postCommentNonce']; | |
// check to see if the submitted nonce matches with the |
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
// if both logged in and not logged in users can send this AJAX request, | |
// add both of these actions, otherwise add only the appropriate one | |
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' ); | |
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' ); | |
function myajax_submit() { | |
$nonce = $_POST['postCommentNonce']; | |
// check to see if the submitted nonce matches with the |
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
jQuery.post( | |
MyAjax.ajaxurl, | |
{ | |
action : 'myajax-submit', | |
postID : MyAjax.postID, | |
// send the nonce along with the request | |
postCommentNonce : MyAjax.postCommentNonce | |
}, | |
function( response ) { |
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
// embed the javascript file that makes the AJAX request | |
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery' ) ); | |
wp_localize_script( 'my-ajax-request', 'MyAjax', array( | |
// URL to wp-admin/admin-ajax.php to process the request | |
'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
// generate a nonce with a unique ID "myajax-post-comment-nonce" | |
// so that you can check it later when an AJAX request is sent | |
'postCommentNonce' => wp_create_nonce( 'myajax-post-comment-nonce' ), |
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
// if both logged in and not logged in users can send this AJAX request, | |
// add both of these actions, otherwise add only the appropriate one | |
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' ); | |
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' ); | |
function myajax_submit() { | |
// get the submitted parameters | |
$postID = $_POST['postID']; | |
// generate the response |
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
jQuery.post( | |
// see tip #1 for how we declare global javascript variables | |
MyAjax.ajaxurl, | |
{ | |
// here we declare the parameters to send along with the request | |
// this means the following action hooks will be fired: | |
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit | |
action : 'myajax-submit', | |
// other parameters can be added along with "action" |
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
// this hook is fired if the current viewer is not logged in | |
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ); | |
// if logged in: | |
do_action( 'wp_ajax_' . $_POST['action'] ); |
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" src="http://example.com/wordpress/wp-content/plugins/myajax/js/ajax.js"></script> | |
<script type="text/javascript"> | |
/* <![CDATA[ */ | |
var MyAjax = { | |
ajaxurl: "http://example.com/wordpress/wp-admin/admin-ajax.php" | |
}; | |
/* ]]> */ | |
</script> |
NewerOlder