It’s already been a while since the WP REST API infrastructure has been merged in to core. In the meantime the development of the full WP REST API continues with the development of the WP REST API plugin. While if you want to use the full power of the WP REST API you still have to install the plugin waiting for it to be merged into core. However if you just need a single API endpoint for your website the infrastructure in core is already sufficiënt for your needs. In this howto I’ll show you how to create a simple custom endpoint in the WP API, in my example the endpoint is used to fill a select2 input field via AJAX. On the other end of the endpoint, data is being received from a third party API via wp_remote_get (cURL). This howto has been created based on WordPress 4.4.1. The following code goes into your themes functions.php file, you can also put the code in a custom made plugin if that suits your needs better. The first thing we need to do is to create the actual endpoint, this is done by registring the endpoint to the WP REST API via the action rest_api_init. Example: In this example we use the function register_rest_route to add the route /search to the WP REST API under the
Share This