Wordpress 改进 REST API - SHORTINIT 不起作用
Wordpress improve REST API - SHORTINIT not work
我正在尝试提高 Wordpress API 自定义端点的性能。
我在 plugin/PLUGIN_NAME 文件夹下创建了一个简单的插件文件,我在其中调用 "register_rest_route" 函数来设置端点。
为了提高性能,我尝试加载的不是所有插件,而是仅加载我需要的 Wordpress CORE 来查询用户和帖子以及终极会员。
那是我的代码:
define('SHORTINIT', true);
require_once dirname(__FILE__) . '/../../../wp-load.php';
require_once dirname(__FILE__) . '/../ultimate-member/ultimate-member.php';
add_action('rest_api_init', function () {
register_rest_route( 'my-api/v1', 'test/me',array(
'methods' => 'POST',
'callback' => 'test'
}
));
...
...
它可以工作,但问题是如果我不加载 "wp-load.php" 脚本,它也可以工作。在我的测试方法中,我使用 WP_User_Query、WP_Query 和最终成员方法,如 um_user().
好像 SHORTINIT 没用。
我错了什么?
阅读wp-settings.php源码发现问题:
// lines 144 to 147 of wp-settings.php
// Stop most of WordPress from being loaded if we just want the basics.
if ( SHORTINIT ) {
return false;
}
// lines 359 to 373 of wp-settings.php
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
wp_register_plugin_realpath( $plugin );
include_once( $plugin );
/**
* Fires once a single activated plugin has loaded.
*
* @since 5.1.0
*
* @param string $plugin Full path to the plugin's main file.
*/
do_action( 'plugin_loaded', $plugin );
}
unset( $plugin );
SHORTINIT 的检查在加载插件之前完成。所以你的 "define('SHORTINIT', true);" 是在检查 SHORTINIT 之后执行的,没有效果。
进一步的 wp-settings.php 是从 wp-load.php 间接包含的,所以当你的插件代码被执行时 wp-load.php 已经被包含了。
谁和我有同样的问题,我推荐使用 Plugin Load Filter,一个 wordpress 插件,让你 select 在 REST API.
中激活哪些插件
我正在尝试提高 Wordpress API 自定义端点的性能。
我在 plugin/PLUGIN_NAME 文件夹下创建了一个简单的插件文件,我在其中调用 "register_rest_route" 函数来设置端点。
为了提高性能,我尝试加载的不是所有插件,而是仅加载我需要的 Wordpress CORE 来查询用户和帖子以及终极会员。
那是我的代码:
define('SHORTINIT', true);
require_once dirname(__FILE__) . '/../../../wp-load.php';
require_once dirname(__FILE__) . '/../ultimate-member/ultimate-member.php';
add_action('rest_api_init', function () {
register_rest_route( 'my-api/v1', 'test/me',array(
'methods' => 'POST',
'callback' => 'test'
}
));
...
...
它可以工作,但问题是如果我不加载 "wp-load.php" 脚本,它也可以工作。在我的测试方法中,我使用 WP_User_Query、WP_Query 和最终成员方法,如 um_user().
好像 SHORTINIT 没用。
我错了什么?
阅读wp-settings.php源码发现问题:
// lines 144 to 147 of wp-settings.php
// Stop most of WordPress from being loaded if we just want the basics.
if ( SHORTINIT ) {
return false;
}
// lines 359 to 373 of wp-settings.php
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
wp_register_plugin_realpath( $plugin );
include_once( $plugin );
/**
* Fires once a single activated plugin has loaded.
*
* @since 5.1.0
*
* @param string $plugin Full path to the plugin's main file.
*/
do_action( 'plugin_loaded', $plugin );
}
unset( $plugin );
SHORTINIT 的检查在加载插件之前完成。所以你的 "define('SHORTINIT', true);" 是在检查 SHORTINIT 之后执行的,没有效果。
进一步的 wp-settings.php 是从 wp-load.php 间接包含的,所以当你的插件代码被执行时 wp-load.php 已经被包含了。
谁和我有同样的问题,我推荐使用 Plugin Load Filter,一个 wordpress 插件,让你 select 在 REST API.
中激活哪些插件