Codex tools: Log in
Contents |
This function can be replaced via plugins. If plugins do not redefine these functions, then this will be used instead.
Verifies the AJAX request to prevent processing requests external of the blog.
<?php check_ajax_referer( $action, $query_arg, $die ) ?>
In your main file, set the nonce like this:
<?php
//Set Your Nonce
$ajax_nonce = wp_create_nonce("my-special-string");
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var data = {
action: 'my_action',
security: '<?php echo $ajax_nonce; ?>',
my_string: 'Hello World!'
};
$.post(ajaxurl, data, function(response) {
alert("Response: " + response);
});
});
</script>
In your ajax file, check the referrer like this:
add_action( 'wp_ajax_my_action', 'my_action_function' );
function my_action_function() {
check_ajax_referer( 'my-special-string', 'security' );
echo $_POST['my_string'];
die;
}
check_ajax_referer() is located in wp-includes/pluggable.php.
Nonce functions: wp_explain_nonce(), wp_nonce_ays(), wp_nonce_field(), wp_nonce_ur(), wp_verify_nonce(), wp_create_nonce(), check_admin_referer(), check_ajax_referer(), wp_referer_field()