Skip to content

Instantly share code, notes, and snippets.

@garyc40
Created October 3, 2012 15:30
Show Gist options
  • Save garyc40/3827580 to your computer and use it in GitHub Desktop.
Save garyc40/3827580 to your computer and use it in GitHub Desktop.
WordPress AJAX & Nonces
// 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' ),
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment