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 marries technical ability with an understanding of the project's bigger picture and the needs of its stakeholders”

Laurence Hughes
Technical Director 2FluidCreative

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.