WordPress Rest API custom endpoint optional param

You should put the named parameters of the route regex into an optional capturing group: register_rest_route( ‘api’, ‘/animals(?:/(?P<id>\d+))?’, [ ‘methods’ => WP_REST_Server::READABLE, ‘callback’ => ‘get_animals’, ‘args’ => [ ‘id’ ], ] ); The second parameter is simply a regex, thus you can use normal regex logic to make it more complex

Does something like is_rest() exist

It’s a good point by @Milo, the REST_REQUEST constant is defined as true, within rest_api_loaded() if $GLOBALS[‘wp’]->query_vars[‘rest_route’] is non-empty. It’s hooked into parse_request via: add_action( ‘parse_request’, ‘rest_api_loaded’ ); but parse_request fires later than init – See for example the Codex here. There was a suggestion (by Daniel Bachhuber) in ticket #34373 regarding WP_Query::is_rest(), but it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)