Skip to content

Instantly share code, notes, and snippets.

View garyc40's full-sized avatar

Gary Cao garyc40

View GitHub Profile
@garyc40
garyc40 / ajax-permission.php
Created October 3, 2012 15:32
WordPress AJAX check permission
// 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
@garyc40
garyc40 / ajax-submit.js
Created October 3, 2012 15:35
WordPress AJAX form submission
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
@garyc40
garyc40 / ajax-enqueue-core-json.php
Created October 3, 2012 15:39
WordPress AJAX JSON
// 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' ) );