Using Ajax

If you want to call the plugin’s shortcode methods as AJAX, then with the standard WordPress calls, you can’t make use of the do_shortcode()  function however you can call the class that builds the shortcode directly.

See a demo of this code in action here.

This is an intermediate developer task, and you will need to have a grasp of PHP / jQuery / javascript / WordPress hooks and function calls,  and the way you implement coding changes in WordPress, either as a custom plugin or within a child theme to be able to modify this example to your needs.

In this example, I have built a search filter and button, of course you can do what you like, such as dynamic tabs.  The example works and is visible on the demo site here, however little attention has been paid to finessing the style etc, as it is expected you are a developer if you are reading this, so you will make it your own.

This is written as a plugin, however, with a few tweaks you could use it in the functions.php for your child theme.

First we will create the html mark up for the search box and results area ( you can put this in page or post for example ).  I have kept this simple

The structure of our plugin directory in this example  is:

  -- test.js
  -- test-ajax.php

You can use what you like, or as mentioned add code to your child theme.  If you are building it as a plugin, once created you can zip the directory up an upload and activate as you would any plugin.

We need our jQuery javascript to make the AJAX calls, you may want to add extra code to give ‘feel’ onclick e.g. ‘loading’ … etc as required

Our PHP code to enqueues the jQuery , add the ajax actions and the function to get the filter and display output

You could easily extend this for instance to switch between list and grid layouts etc.

Full description of making ajax calls in WordPress can be found in the WordPress Developer Codex.

Was this helpful?