- Adding ajaxurl for the public side (optional)
- Preparing callable functions
- Hook for the admin side
- Hook for the public side (optional)
- jQuery implementation
This step is optional. You may need it only for ajax implementations on the public front-end.
add_action('wp_head', 'add_public_ajaxurl');
function add_public_ajaxurl() {
?><script type="text/javascript">var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";</script><?php
}
add_action("wp_ajax_{$action}", "callable_function"); // wp_ajax_register
This step is optional. You may need it only for ajax implementations on the public front-end.
add_action("wp_ajax_nopriv_{$action}", "callable_function"); // wp_ajax_nopriv_register
var data = {
action: 'register',
key_1: 'value_1',
key_2: 'value_2'
};
jQuery.post( ajaxurl, data , function( json ) {
console.log( json );
}, "json");
jQuery.get( ajaxurl, data , function( json ) {
console.log( json );
}, "json");