How to use AJAX to call a function in WordPress

This might be old stuff for many people out there and I know that most things can be accomplished with the amazing WP API, still I think it’s good to have a simple example of using AJAX to call a WordPress function in one post. Here we go!

Scenario:

You want to request some data from WordPress (e.g. the previous post ID), through an AJAX call.

How to do this:

The way I solved this was to add a global variable in the header, so that the WordPress AJAX URL is exposed. Then I added a function in my theme’s function.php file, which would do the actual work. Finally, in my JavaScript file it’s just a matter of calling my function with AJAX. I can then use the result to do what I need.

In header.php

In function.php

In script.js

 

Leave a Reply

Your email address will not be published. Required fields are marked *