Download the PHP package anthonybudd/wp_ajax without Composer
On this page you can find all versions of the php package anthonybudd/wp_ajax. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package wp_ajax
WP_AJAX
Simple Controllers for WordPress
WP_AJAX is a class for interfacing with WordPress’s built in AJAX system. This class is designed to fully abstract the developer from the traditional hook based system to a clean controller style structure. Simply define the class, echo urls with url($params = []) method and write what code you want to execute in the run() method.
Introduction: Medium Post
ExampleAction.php
Font-end
Page-Template.php
Or
script.js
Installation
Require WP_AJAX with composer
Or
Download the WP_AJAX class and require it at the top of your functions.php file.
Setup
You will need to create a new class that extends WP_AJAX. This class must have one protected property called $action and one protected method named run(). $action will be the AJAX action name See wpajax(action).
Listen
Next you have to call the static method listen(). This will create all of the hooks so WordPress knows to call the run() method when the correct AJAX endpoint is hit. Note: You will need to call the listen() method for each of your AJAX actions.
If you would like to only allow signed in users to access your AJAX endpoint add the argument FALSE to the listen method.
JSON Response
If you want to respond to an AJAX request with data the JSONResponse() method will automatically set the content type header to ‘application/json’ and JSON encode the data provided to the method.
I am aware that WordPress has a function called wp_send_json() but, due to the fact that I know how much it annoys WP developers that I have included this method, I will not be removing it.