enfix
enfix

Reputation: 6980

Wordpress improve REST API - SHORTINIT not work

i'm trying to improve the performance of Wordpress API custom endpoints.
I created a plugin, simple file under plugin/PLUGIN_NAME folder where I call "register_rest_route" function to set the endpoints.
To improve the performance I am trying to load not all the plugins, but only what I need, Wordpress CORE to query users and posts and Ultimate Members. That's my code:

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'
     }
  ));
...
...

It work, but the problem is that works also if I not load "wp-load.php" script. In my test method I use WP_User_Query, WP_Query and ultimate member method like um_user().
It seems like SHORTINIT didn't work.
What I wrong ?

Upvotes: 2

Views: 2444

Answers (2)

enfix
enfix

Reputation: 6980

Who have my same problem, I recommend using Plugin Load Filter, a wordpress plugin that let you select which plugins to activate in the REST API.

Upvotes: 1

user8717003
user8717003

Reputation:

Reading the source code of wp-settings.php shows a problem:

// 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 );

The check for SHORTINIT is done before plugins are loaded. So your "define('SHORTINIT', true);" is executed after not before SHORTINIT is checked and has no effect.

Further wp-settings.php is included indirectly from wp-load.php so when your plugin code is executed wp-load.php has already been included.

Upvotes: 2

Related Questions