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
Related Posts:
- How to stop WP API endpoint from caching?
- “rest_no_route” – Debug error
- How to add an endpoint for my custom post type? /wp-json/wp/v2/posts is working but it didn’t in the custom post
- Using the Rewrite API to Construct a RESTful URL
- How do I cache (core) API requests?
- WP REST API only returning partial list of users
- Slow REST API calls on self-hosted WordPress [closed]
- Retrieving pages with multiple tags using REST API
- how to authenticate for the REST API from a plugin and from command line
- WordPress API – Get Drafts
- How do I use the WP REST API plugin and the OAuth Server plugin to allow for registration and login?
- How to get the attached gallery in the rest API?
- WP API : date_query parameter
- Sync posts from one WordPress site to another
- Custom route for WP-API gives “rest_no_route” value
- Get more than 10 posts in a specific category with the wordpress api
- Developing Themes with React/Angular
- curl POST work with user meta but not the custom user meta
- Implementing the wp json-rest api
- Create post with REST API in php with file_get_contents
- Can’t get POST data in API endpoint callback
- node-wpapi: how to handle authentication?
- How can I cache WordPress Rest API Response
- WordPress REST API always misses parameters – rest_missing_callback_param
- Adding rest api endpoints to an old theme
- Develop REST API using WordPress for Android app [closed]
- Why aren’t tags and categories added in post request to WP Rest API
- POST request not going through?
- Does it still make sense using json endpoint ep_mask now that there’s the new rest api? [closed]
- Calling an API to do authentication / user login
- How do I query my WordPress posts on another HTML page using the WordPress Restful API?
- body_class REST field in WP-API
- How to edit feature image with XML RPC?
- How to fix AWS SNS API end point code that cause site not to load?
- WP REST API and Access-Control-Allow-Origin
- Custom Endpoint – Does it possible to use PUT method with WP API Rest?
- rest_cannot_create_user
- Periodically Query an external API on wordpress site
- Extending the API I created to return category names
- Rest WP_Error always return null
- Posting data from Ionic app to WordPress
- Expand author, tags and categories in the WordPress JSON API
- How to display parent category with first level child in REST API
- WordPress REST API Endpoint (Authors and Categories latest posts)
- Why does “if statement” has to “die()”, otherwise wont work?
- Best way to make a JSON API from WordPress?
- REST API And Error Codes – No Message
- How to login via wordpress api and get user details?
- How many users/load can the rest API handle?
- Postman Resolving “Invalid CORS request” for a POST Request
- Set up WP Authentication from External API
- WordPress JSON output
- Is it safe to fix Access-Control-Allow-Origin (CORS origin) errors with a php header directive?
- How can I get users email (and additional data) from the rest API?
- External WordPress API
- REST API: How can I restrict a custom post type to only be accessible by authenticated users?
- Gutenberg internal page link search box
- WordPress Rest API – Get all posts based on post_meta on custom endpoint
- Does WordPress support a shortcode calling itself from within a shortcode call?
- REST-API: extend media-endpoint
- How to get WordPress Plugins Data in PHP using Plugin API?
- How to integrate Yelp API
- Where to find WordPress API changes for each version released
- Get all tags not just first 10 with wp api 2.0
- Unable to find API Key for my blog
- Passing a borrowed nonce through Postman fails
- How to add WP API and JS featured image attachment
- Get all PDF files from page with WordPress API
- WC REST Payment – Stripe Gateway’ is not available
- How to query WordPress from another application?
- How to set a template with wp_insert_post
- I can’t post comment via REST API
- Multisite WP-API json v2 : can i fetch all comments on all sites with a single query?
- Soundcloud Smart Player
- Jetpack JSON API configure button not showing [closed]
- New Twitter API and Twitter count + timeline doesn’t work anymore [closed]
- wp_signon returns user but the user is not logged in because wp installed on subdomain. How to make it work?
- get_pages and get_posts returning different data?
- Working Around rest_forbidden_context
- WP_REST_Response – How to return Gzip answer and add Content-encoding header?
- WordPress REST API – get custom taxonomy category posts
- Post content in wp_remote_post
- Can I overwrite default WordPress Json API For no more pages text
- I need some assistance opening a few videos in VLC on android automatically from my wordpress site
- Remove unwanted fields from WP API response
- WordPress API “code”:”rest_no_route” with Custom Route
- Get total count of records in WP GraphQL API
- WordPress output data to another website and pull data
- Intergrate JSON into wordpress
- Need to forward Data from WooCommerce Webhook sent to same site WordPress REST API custom endpoint
- Set logged in user based on API response
- Gravit Forms Notification send after 3rd party API response
- Rest API User Levels
- Trying to integrate wordpress query on other website – getting redirection to wp-install
- Zoho subscription API to WordPress [closed]
- How to update WordPress Plugins in your own maintance application?
- Update post / page using API + python
- Is it possible to create post in wordpress using postman?
- Issue with API after 6.2 update
- Using WordPress API to mass update posts freezes the server