admin管理员组文章数量:1277580
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.
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.
1 Answer
Reset to default 2You 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);
}
本文标签: rest apiUnderstanding SHORTINIT with Wordpress 5
版权声明:本文标题:rest api - Understanding SHORTINIT with Wordpress 5 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741278966a2369896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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. – Jacob Peattie Commented Jan 30, 2019 at 12:47SHORTINIT
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. – Jacob Peattie Commented Jan 30, 2019 at 12:47wp-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 – Alexander Holsgrove Commented Jan 30, 2019 at 12:52