7

What I am trying to achieve

I am looking at using Wordpress decoupled, and most tutorials and guides go into detail on using the Rest API. Recently, I discovered the SHORTINIT option to load a very minimal Wordpress instance. There is next to no documentation, and very few questions about its use, however I am interested in making use of it.

What have I tried

So far, I have setup a simple test php file in the root directory:

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);
//define('DOING_AJAX', true);
define('SHORTINIT', true);

require(dirname( __FILE__ ) . '/wp-load.php');

require( ABSPATH . WPINC . '/post.php');

die(json_encode(get_posts(array(
   'numberposts' => 10,
   'post_type' => 'post'
))));

Which failed because I can't access class WP_Query. After reading a few scattered examples, I gradually added more requires but nothing seemed to work.

I have now taken a copy of wp-settings and removed things like class-wp-rest*.php or references to the themes. The intention being to leave myself with the absolute minimum core, with enough functionality to obtain the data I need (posts, custom fields). My code now runs, and takes 1/3 of the time to load my posts compared to the /wp-json/wp/v2/posts endpoint.

My Question

I am pre-empting people telling me just to use the REST API, but I would like to know if there is any documentation or examples for creating an endpoint URL which makes use of the SHORTINIT, explaining which core components I need.

6
  • Have a look at the wp-settings.php file. SHORTINIT is checked on line 134. Look at everything that is loaded after that. The verison of WordPress loaded with SHORTINIT is very minimal. It doesn't even load the WP_Query class or any of the REST API. So you're missing out on a lot if you do it this way. For example, if you have any plugins that in any way modify post data or change how they're queried or anything, then that won't be loaded in your endpoint. So forget querying custom post types. Commented Jan 30, 2019 at 12:47
  • 2
    SHORTINIT is the sort of thing you might use if you just need a handful of utility functions and access to the database object. It's not something you want to use if you're trying to use large chunks of WordPress functionality. Commented Jan 30, 2019 at 12:47
  • Thanks @JacobPeattie. I have looked through wp-settings.php and copying that into my test file lets me use get_posts and the like. I want to be able to gradually remove components that I don't need, so that less is loaded and hence gives better performance. Aside from manually removing each require by trial and error, I was hoping there would be some sort of guide. Thanks again Commented Jan 30, 2019 at 12:52
  • Honestly, it sounds like you're over-engineering this. What is the code you actually want to run at your endpoint? How much have you optimised that? Commented Jan 30, 2019 at 12:54
  • We've already done a lot with front-end performance, and have had good results with plugins such as WP SuperCache. The next thing to explore is react and the REST API, but I wondered if this method, in certain situations I stress, could be used to give a bit better performance over the API. Also, it's something new so I wanted to have a tinker! Interested to hear if you've used it at all. Totally agree with the over-engineering comment! Commented Jan 30, 2019 at 13:08

1 Answer 1

2

You could test if the request is coming in through the rest API before enabling the SHORTINIT

Something like...

if (strpos($_SERVER[ 'REQUEST_URI' ], '/wp-json/') !== false) {
    define('SHORTINIT', true);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.