Make WordPress WP-API faster by not loading the theme and plugins

I would like to make requests to the WordPress API much faster. My API is implemented in the plugin (using register_rest_route to register my routes). However, since this is a plugin, everything loads with it (child theme and theme), and basically the request to this API takes half a second due to loading all of these useless parts.

Is the WordPress API used differently? Since most plugins using the WP-API do not need any other plugins to download, the more so the theme ... I don’t understand how they could have missed this.

Is there any way to do this?

+4
source share
4

, . , ​​WordPress (DB ), :

<?php

define('SHORTINIT', true);  // load minimal WordPress

require_once PATH_TO_WORDPRESS . '/wp-load.php'; // WordPress loader

// use $wpdb here, no plugins or themes were loaded

PATH_TO_WORDPRESS, ; . , :

require_once dirname(__FILE__) . '/../../../wp-load.php'; // backwards 'plugin-dir/plugins/wp-content'

SHORTINIT true, , .

WP_DEBUG , WordPress, :

  • SHORTINIT: ~ 0,045
  • SHORTINIT: ~ 0,0015

, , , , OpCache (, APC PHP OpCache ).

, 2 SHORTINIT wp-load.php, .

, , WordPress ( Ajax ). WP.

EDIT: OP WP-API, WordPress , . , - .

WP API , @David , , , - .

12 api, "" , 25 (, , ). index.php WordPress microtime(true) , , REST, , , API.

0.0462 - 0.0513 ( PHP OpCache ). , WordPress .

, , . , , .

+8

, .

php , db, , , 500 . ( wp ..), , api, mu-plugin. exit, .

: 1. mu-plugins wp-content (, )

  1. api-cache.php

  2. :

    function get_api_cache(){
    
        //dont run if we are calling to cache the file (see later in the code)
        if( isset($_GET['cachecall']) && $_GET['cachecall'] === true)
            return;
    
        $url = "$_SERVER[REQUEST_URI]";
    
        //do a little error checking
    
        $uri= explode('/',$url);
    
        //we have a array (1st key is blank)
        if( $uri[1] !== 'wp-json' || $uri[2] !== 'wp' || $uri[3] !== 'v2'){
            return;
        } 
    
        //lock down the possible endpoints we dont want idiots playing with this...
        $allowed_endpoints= array(
            'posts'
        );
    
        $endpoint= array_pop($uri); // not sure if this is valid or not, is there more structure to some api calls?
    
        if( !in_array( $endpoint, $allowed_endpoints) ){
            return;
        } 
    
        //ok reasonably confident its a api call...
    
        $cache_folder= get_stylesheet_directory().'/api_cache/';
    
        // prob best if not within php server but to get you going
        if(! file_exists ( $cache_folder ) ){
            mkdir($cache_folder); //warning 777!!
        }
    
    
        /*
        * Need to choose a method of control for your cached json files
        * you could clear out the folder on update post/ taxonomies etc
        * or cron clear out hourly/weekly whatever freq you want
        */
    
    
        if( file_exists($cache_folder.$endpoint.'.json') ){
            $json= file_get_contents($cache_folder.$endpoint.'.json');
            header('Content-Type: application/json');
            echo $json;
            exit;// we need nothing else from php exit 
        } else {
            //make sure there will be no errors etc..
            $ch = curl_init();
            $url= "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?cachecall=true";
            $timeout= 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $json = curl_exec($ch);
            curl_close($ch);
            file_put_contents($cache_folder.$endpoint.'.json', $json);  
        }
    
    }
    
    get_api_cache();    
    

( , ).

:

  • , - 777, , , http .
  • , , , hook/filter .
  • . , , , cron, (, 3 , , 10 , .. - ?) , , ..
  • ( if, , , 404s !)
+2

. , / -, .

, , - .php, - API.

, DB?

0

, .

Wordpress.

/public_html/my-plugin/my-plugin.php

and include wordpress main file.

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

/public_html/wp-content/plugins/my-plugin/my-plugin.php
require_once dirname(__FILE__) . '/../../../wp-load.php';

, wp-load.php .

wp-settings.php , . Wordpress mu-plugins (wp-content/mu-plugins/) hook muplugins_loaded. , . , action hook muplugins_loaded, script.

SHORTINIT wp-load.php, DB, . , , .

// file my-plugin.php
//call before include file wp-load.php
global $wp_filter;
$wp_filter = array( 
    // pass wp hook where to want exit extra wp loaded
    'muplugins_loaded' => array(
     // prority
         1 => array(
            // callback function register
            'wp_extra_loaded_exit' => array( 

              'function' => 'wp_extra_loaded_exit',

              'accepted_args' => 1

              )
         ) 
   )
);



function wp_extra_loaded_exit(){
  exit; 

}
require dirname( dirname( __FILE__ ) ).'/wp-load.php';
// plugin code here.

We check that the muplugins_loaded hook defines wordpress earlier, you can also determine which hook is detected before muplugins_loaded , then stop this point after loading more Wordpress files. -

If you want to test your script file wp-settings.php and find the line muplugins_loaded, then you can check the echo instruction.

echo "Wordpress loaded in this point before"; 

do_action( 'muplugins_loaded' );

echo "After this wordpress not loading"; // Output fail bcz we exit 
0
source

All Articles