WordPress Rest API custom endpoint optional param. There may be a way to do it with one register_rest_route
function call, I do not know how to do that and it would be ideal. However, duplicating the register_rest_route
function call in the hooked method will do what you want.
1 2 3 4 5 6 7 8 9 10 11 12 | register_rest_route( 'api', '/animals/', [ 'methods' => WP_REST_Server::READABLE, 'callback' => 'get_animals' ] ); register_rest_route( 'api', '/animals/(?P<id>\d+)', [ 'methods' => WP_REST_Server::READABLE, 'callback' => 'get_animals', 'args' => [ 'id' ], ] ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.