30

I have an issue with the speed of the WordPress REST API. What I’m trying to do is get data for a report about 26k records in total as fast as possible to give the user a fluid user experience. The issue I’m running into is it seems that WordPress loads core, plugins and themes when the REST API is called.

Table

I’ve run out of ways I know to optimize the code, is there some WordPress tweaks anyone knows to improve the speed? Are these results normal for people using the REST API? As you can see the time to run my code isn't the issue but the WordPress overhead is.

Tony
  • 859
  • 1
  • 7
  • 19
  • 26k records sounds like a lot. Why do you need that many? – Jim Wright Jul 31 '17 at 17:34
  • Hi Jim, it's part of a reporting system that reports on a certain plugin's data for my company. We don't get all 26k records at once all the time and we do have paging system built in. I used the 26k record as a point to show that the major of the time taken up for the request is is not my code for the request but is WordPress's code. Even if i grab no records and return null the request time will still be very high, a quick test with all the plugins enabled and the theme set to DIVI i'm getting a responds time of 1130ms. – Tony Jul 31 '17 at 17:56
  • As far as I know, the REST API goes thru the Wordpress rewrite process, thats why the whole theme and plugins are loaded. In an Ajax call you can define('SHORTINIT',1) before to include wp-load.php, but with REST I never found any similar option. – Gustavo Jantsch Jul 31 '17 at 18:11
  • Thanks for the help, i ended up doing a ajax call to file that loaded only part of WordPress core – Tony Jul 31 '17 at 20:53
  • Another thing you might try is disabling Wordpress WP-Cron: https://developer.wordpress.org/plugins/cron/ – Flimm Nov 20 '17 at 08:33

3 Answers3

26

Overview: So the issue is a limitation of WordPress as of version 4.8. WordPress is designed to load plugins and themes and all of its core every REST API request. Here is the reason for the slow response time.

Solution: The only current solution is an ajax call to a file in your plugin and loads only part of the WordPress core. The code below is direct file access while still being able to use WordPress functions with fast response time.

//Tell WordPress to only load the basics
define('SHORTINIT',1);

//get path of wp-load.php and load it
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';

// register global database
global $wpdb;

// return data selected from DB to user

Results: Response times are down to 100ms. That's a huge difference from 1069ms to 108ms.

Reference: https://deliciousbrains.com/wordpress-rest-api-vs-custom-request-handlers/

Last notes: The Wordpress REST API is very new, quite powerful and you should be using in most situations where response time is not an issue.

Tony
  • 859
  • 1
  • 7
  • 19
  • 2
    Where would you define this? – Advena Nov 12 '18 at 09:08
  • 2
    This would be defined in your code when you are building a custom plugin – Tony Nov 13 '18 at 00:20
  • 1
    How about when I am not building a custom plugin? Do you know where should I add this code to improve wordpress api response time? – redshot Nov 13 '18 at 06:30
  • @ConanCarroll This code needs to be used in the creation of a custom file, by its self i wont do anything but load the bare minumum from WordPress. You'd use that code in a custom PHP file, then use the global wpdb to make any access calls you need to the database then dump what ever json you needed. It would be much faster because it only loads what you want to load, but it's a very advanced and time consuming way to go. – Chris Morris Nov 20 '18 at 11:32
  • I haven't tested this out, but does this mean I need to write custom routes to get Posts, and Pages now essentially negating what WP-Rest has already built? – Spencer Bigum Jan 30 '19 at 23:26
  • 2
    This is bypassing the REST endpoint that WordPress uses. You can't register it , you would call this file directly – Tony Mar 09 '19 at 17:14
  • 1
    In my case it went down to 10ms Why WordPress does load all the stuff on every request? while rest apis were a new concept in wordpress and they could architecture it way better than this, they could build it in a way that wordpress features could be loaded on demand and solely and you don't have to load the entire codebase on every request – Amin Jun 06 '22 at 15:40
3

If response time is critical for your app, and you don't mind spending the time and effort, i would recommend creating your own entry point to retrieve the data you need. Baseline for this method is illustrated in the following article: https://medium.com/@yairlevy/wp-rest-api-too-slow-2da859f3cc93

Yair Levy
  • 1,354
  • 16
  • 12
2

I've just found this plugin -> https://wordpress.org/plugins/wp-rest-cache/

This is really time-saving plugin and tested on our live website.

Results: 1200ms went down for 50ms average.

Uranbold
  • 45
  • 6