通过不加载主题和插件使WordPress WP-API更快 [英] Make WordPress WP-API faster by not loading theme and plugins

查看:153
本文介绍了通过不加载主题和插件使WordPress WP-API更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更快地向WordPress API发出请求.我的API是在插件中实现的(使用register_rest_route来注册我的路线).但是,由于这是一个插件,因此所有内容(子主题和主题)都已加载,并且由于所有这些无用的部分都已加载,因此对该API的查询基本上要花半秒钟的时间.

I would like to make requests to the WordPress API much faster. My API is implemented in a plugin (using register_rest_route to register my routes). However, since this is a plugin, everything is loaded with it (the child-theme and the theme) and basically a query to this API is taking half a second because of all this useless parts loaded.

WordPress API不能以其他方式使用吗?由于大多数使用WP-API的插件不需要加载任何其他插件,甚至不需要主题,我不知道他们怎么会错过它.

Doesn't WordPress API can be used in another way? Since most plugin making use of the WP-API doesn't need any other plugins to be loaded, even less a theme... I don't understand how they could miss that.

反正有这样做吗?

推荐答案

是的,有可能.在我需要最小的WordPress核心(没有插件和主题的数据库)的其中一个插件中,我要做的是:

Yes, it is possible. In one of my plugins where I need the minimal WordPress core (DB without plugins & themes) here is what I do:

<?php

define('SHORTINIT', true);  // load minimal WordPress

require_once PATH_TO_WORDPRESS . '/wp-load.php'; // WordPress loader

// use $wpdb here, no plugins or themes were loaded

我组成的PATH_TO_WORDPRESS常量;您只需要指出正确的路径即可.例如,在插件中,它可能看起来像:

The PATH_TO_WORDPRESS constant I made up; you just need to point that to the correct path. In plugins for example, it might look like:

require_once dirname(__FILE__) . '/../../../wp-load.php'; // backwards 'plugin-dir/plugins/wp-content'

SHORTINIT设置为true当然可以提高性能.

Setting SHORTINIT to true certainly does help performance a bit.

在禁用WP_DEBUG的情况下,引导WordPress所需的时间如下:

With WP_DEBUG disabled, the time it takes to bootstrap WordPress are as follows:

  • 没有SHORTINIT:〜0.045秒
  • 使用SHORTINIT:〜0.0015秒

如果这是您自己需要性能的站点,则可以通过启用OpCache(例如,最新版本的APC或PHP OpCache)来提高此性能.

If this is for your own site where you demand performance, you can probably increase this a bit by enabling an OpCache (e.g. APC or PHP OpCache in recent versions).

但是我相信上面要定义的两行代码SHORTINIT并要求wp-load.php是您想要的.

But I believe the 2 lines of code above to define SHORTINIT and require wp-load.php are what you're looking for.

为澄清起见,此文件是插件的一部分,但它的调用独立于WordPress本身(通过Ajax和直接).插件或WP本身的任何其他部分都不会包含或使用它.

To clarify, this file is a part of a plugin, but it is called independently of WordPress itself (via Ajax and directly). It never gets included or used by any other parts of the plugin or WP itself.

编辑:由于OP实际上与WP-API有关,而不是与WordPress有关,因此,我添加此内容是为了解决实际问题.我将保留原始答案内容,以防其他人帮助.

Since the OP is actually concerned with the WP-API, not WordPress in general, I am adding this to address the actual question. I'll leave the original answer content in case it can help someone else.

我使用WP API进行了进一步测试,就像@David在他的回答中说的那样,问题可能出在其他方面.

I did further testing with the WP API and like @David said in his answer, the issue is probably something else.

除了其余的api,我还加载了12个插件,一些相当大"的插件,并且我的本地安装已安装了大约25个主题(当然一个是活动的).我编辑了WordPress的index.php文件,并使用microtime(true)记录了一切开始的时间,然后编辑了一个REST控制器,以计算从开始到到达API端点所需的时间.

I loaded up 12 plugins in addition to the rest api, some fairly "large" plugins, and my local install has about 25 themes installed (one active of course). I edited WordPress' index.php file and used microtime(true) to record when everything started, and then edited one of the REST controllers to calculate how long it took from start to getting to the API endpoint.

我的系统上的结果始终在0.0462-0.0513秒左右(没有PHP OpCache,也没有其他系统负载).因此,引导所有WordPress似乎对性能几乎没有影响.

The result on my system is consistently around 0.0462 - 0.0513 seconds (no PHP OpCache, and no other system load). So it appears bootstrapping all of WordPress has little impact on performance.

如果请求花了半秒钟,那么瓶颈就在其他地方,切出插件和主题将产生最小的影响.至少这是我发现的.

If the requests are taking half a second, the bottleneck is elsewhere and cutting out plugins and themes is going to have minimal impact. At least this is what I found.

这篇关于通过不加载主题和插件使WordPress WP-API更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆