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

 

"solid ability in all aspects of creating a successful user interface"

Denise Ross
UX Designer Barclays Bank PLC

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.