How to do Ajax in WordPress

By October 19, 2016 Ajax, Javascript, Web Design, Wordpress

updated 10th Jan 2018

Write a javascript function to trigger the ajax call

Put js above in my_js_file.js (see later in this post).

Wrap in code below for nice loading:

jQuery( document ).ready(function() {    put above code here     });

To see how to send a form via ajax see here.

 

Write a php function to handle the ajax call (you could put this in functions.php)

 

There is one final piece, you might notice the ajax_object.ajax_url in the Javascript function above. We need to make this available in the js file that this function is in. This needs to be done in php.

Include ajax_url like this (below)

 

Leave a Reply