We’re still coming across newly written code from WordPress developers that use admin-ajax.php for AJAX requests. The WordPress REST API was released almost ten years ago and it’s faster, more secure, and significantly easier to work with.
Treat admin-ajax.php like you’d treat jQuery: legacy code that shouldn’t appear in new projects.
There are two pretty great things with the REST API that you may want to consider mentioning.
They are cacheable, you can use a plugin like WP Rest Cache to turn that 1.5 second call into a 100 ms call. Pretty much useless for dynamic stuff, but that page 4 in the article infinite scroll benefits greatly from being cached. You can also cache stuff with Cloudflare if that’s enabled for the site.
You can prevent plugins from running for your API to make them even faster. This is done using an MU-plugin that loads before the rest of the plugins. Something like this will do:
disable-plugins.php:
add_filter('option_active_plugins', function ($plugins) {
// Only touch REST API requests
if (!defined('REST_REQUEST') || !REST_REQUEST) {
return $plugins;
}
$route = $_SERVER['REQUEST_URI'] ?? '';
// Endpoints where we want no normal plugins at all
$endpoints_no_plugins = [
'/wp-json/wt/v1/beverages',
];
// Check for any specific endpoint
foreach ($endpoints_no_plugins as $endpoint) {
if (strpos($route, $endpoint) === 0) {
return []; // Disable all plugins
}
}
// Otherwise, leave plugins alone
return $plugins;
});
Looking forward to seeing some example APIs being added to Radicle to see how close to my spagetti you end up being!