Typically admin-ajax has been the ‘go to’ for handling asynchronous requests within WordPress. Since WordPress 4.4 we now have the ability to use the WP REST API to handle such requests. The new WP REST API introduced in 4.4 allows us to create structured RESTful interfaces for these requests and removes a lot of the overhead that comes with loading admin-ajax. I’d like to illustrate the differences between the approaches with a code comparison. For this example, I’ll be creating a simple form which validates an email address using the WordPress is_email function. Don’t forget to read the comments in the code as I’m primarily using this to show where the approaches differ.
I won’t be going into detail about how to manage AJAX requests in WordPress or the specifics of creating RESTful interfaces with WP REST API.
The form
Just some simple HTML markup here:
<form id="form">
<input type="text" id="email" />
<input type="submit" />
</form>
The admin-ajax way
First, we enqueue the JS file and then use wp_localize_script to provide a global
Source: https://managewp.org/articles/13878/beyond-admin-ajax-using-the-wordpress-rest-api
source https://williechiu40.wordpress.com/2016/11/22/beyond-admin-ajax-using-the-wordpress-rest-api/
No comments:
Post a Comment