WordPress Ajax: The Easy Way

This is a simple guide to implement an AJAX to your WordPress site.

Quick Summary:

  1. Register your Javascript file to WordPress and localize your AJAX admin URL.
  2. Setup simple jQuery script to send post to admin URL.
  3. We also need setup the action function to handle the request.

1) Register your Javascript file to WordPress and localize your AJAX admin URL

You may add it to your theme’s functions.php

I created a script.js located theme /assets/js/script.js. I localized AJAX URL using wp_location_script() functions

2) Setup simple jQuery script to send post to admin AJAX URL

3) We also need setup the action function to handle the request

You can handle the values using $_GET or $_REQUEST method.

 

Leave a Reply

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