Practical REST APIWhy admin-ajax.php should be avoided and how
Aki Björklund CTO,H1
@akibjorklund
Communication between JavaScript and PHP is done with Ajax
Historically that is done in WordPress by calling
/wp-admin/admin-ajax.php
Why admin-ajax.php is bad?
1. Inefficient
2. Not cached
3. No infrastructure like authentication, data
validation, …
4.Ad hoc: messy
WordPress REST API in core from 4.4
There are no built-in endpoints yet
Unless you install the REST API plugin
In many cases no need to write any PHP to do a lot.
But what can you do just with what the core already has?
Example: Comment Reactions
Converting admin-ajax.php to REST API
Before
After
Best practice is to implement what you just saw as a REST Controller. Currently available only with the REST API plugin.
Summary
Use the REST API instead of admin-ajax.php
It’s faster and cleaner
It’s also both easy to implement and migrate to
.
Aki Björklund CTO,H1
@akibjorklund