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;
}

 

"Iain had listened and latched on to a key element in the brief - simplicity"

Alister Thomson
Director SPM

Think we may be able to help you? Why not start a conversation - chances are, you'll go away with some new ideas and knowledge.