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?