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 require
s 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.
SHORTINIT
is checked on line 134. Look at everything that is loaded after that. The verison of WordPress loaded withSHORTINIT
is very minimal. It doesn't even load theWP_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.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.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 eachrequire
by trial and error, I was hoping there would be some sort of guide. Thanks again