AJAX in WordPress: A template
To implement AJAX in wordpress themes, use the following template:
Javascript
var data = array();
data[‘action’] = ‘FUNCTION_NAME’;
new Request.HTML({
url: ‘/wp-admin/admin-ajax.php’,
update: $(‘results’)
}).post(data);
PHP
add_action(‘wp_ajax_FUNCTION_NAME’, ‘FUNCTION_NAME’);
add_action(‘wp_ajax_nopriv_FUNCTION_NAME’, ‘FUNCTION_NAME’);
function FUNCTION_NAME() {
…logic…
exit;
}