$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>rest_api_init is run on every rest call to endpoint|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

rest_api_init is run on every rest call to endpoint

matteradmin9PV0评论

I noticed that every time I make a call to a rest endpoint, the register_rest_route method was called. I made the following very simple plugin to make sure that I was not the culprit, but it has the same behaviour, this is the plugin:

/**
 * Plugin Name: WP REST test
 * Description: Example
 * Author: Tester
 * Author URI:
 * Version: 1.0.0.
 * Plugin URI:
 * License: MIT
 */

class RestController extends WP_REST_Controller {
    public function register_routes()
    {
        register_rest_route('example/v1', '/read', array(
            array(
                'methods' => WP_REST_Server::READABLE,
                'callback' => array($this, 'readdata'),
                'args' => array(),
            )
        ));
    }
    function readdata(){
        return new WP_REST_Response('OK');
    }
}
function register_rest_controller() {
    error_log("Register RestController", 0);
    $controller = new RestController();
    $controller->register_routes();
}
add_action( 'rest_api_init', 'register_rest_controller' ); 

As you can see its only function is to return the string 'OK' when the endpoint is called. When I call http://localhost/wp-json/example/v1/read it works fine, but every time I access this url, the 'Register RestController' text is written to the log. This means that every time the endpoint is called, the rest route is registered. The term 'register' however suggests this should happen only once.

Is this the usual behaviour, or am I doing it wrong?

I noticed that every time I make a call to a rest endpoint, the register_rest_route method was called. I made the following very simple plugin to make sure that I was not the culprit, but it has the same behaviour, this is the plugin:

/**
 * Plugin Name: WP REST test
 * Description: Example
 * Author: Tester
 * Author URI:
 * Version: 1.0.0.
 * Plugin URI:
 * License: MIT
 */

class RestController extends WP_REST_Controller {
    public function register_routes()
    {
        register_rest_route('example/v1', '/read', array(
            array(
                'methods' => WP_REST_Server::READABLE,
                'callback' => array($this, 'readdata'),
                'args' => array(),
            )
        ));
    }
    function readdata(){
        return new WP_REST_Response('OK');
    }
}
function register_rest_controller() {
    error_log("Register RestController", 0);
    $controller = new RestController();
    $controller->register_routes();
}
add_action( 'rest_api_init', 'register_rest_controller' ); 

As you can see its only function is to return the string 'OK' when the endpoint is called. When I call http://localhost/wp-json/example/v1/read it works fine, but every time I access this url, the 'Register RestController' text is written to the log. This means that every time the endpoint is called, the rest route is registered. The term 'register' however suggests this should happen only once.

Is this the usual behaviour, or am I doing it wrong?

Share Improve this question asked Feb 25, 2019 at 9:23 TjeerdTjeerd 231 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

This is the expected behaviour. When you register routes with the REST API you're registering them for the current request. This is the standard pattern for WordPress. It's the same behaviour with register_post_type(), register_taxonomy(), register_setting(), register_nav_menu(), register_widget() and so on. Actions and filters registered with add_action() and add_filter() are also registered — and fire — for every request.

Of course this makes sense when you consider that none of these functions write anything to the database, which is the only way it would possible for these functions to only run once. It makes more sense when you consider that reading the values from these functions from the database would only make them slower. Not to mention that quite often the arguments passed to these functions are instances of classes or class methods, which are things that you couldn't save to the database.

Post a comment

comment list (0)

  1. No comments so far