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(
                '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();
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?

1 Answer 1


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.

  • Ah OK, thanks, that certainly makes sense. Thank you for your reply. Now I can continue without feeling worried I am doing something wrong :-)
    – Tjeerd
    Commented Feb 25, 2019 at 11:59

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.