Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress REST API Slow Response time

Tags:

php

wordpress

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.

like image 642
Tony Avatar asked Jul 31 '17 17:07

Tony


People also ask

Is WordPress REST API fast?

We use REST-API for our Android and iOS apps to get posts, etc., without caching this costs a lot of CPU. With this plugin it takes NO CPU and it's really fast!

Should I disable WordPress REST API?

However, most website owners do not need these features, and it may be smarter to disable the WordPress JSON REST API. No one can deny the benefits that this API brings to WordPress developers. Simply put, it allows developers to retrieve data very easily using GET requests.

Can I use REST API with WordPress?

In particular, the WordPress REST API enables you to connect your WordPress website with external applications. This means you can develop a mobile app using practically any programming language, and use the WP REST API to fetch data from WordPress.


3 Answers

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.

like image 86
Tony Avatar answered Sep 18 '22 07:09

Tony


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

like image 34
Yair Levy Avatar answered Sep 21 '22 07:09

Yair Levy


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.

like image 41
Uranbold Avatar answered Sep 22 '22 07:09

Uranbold