A tutorial on how to extend the WP core REST API with a custom endpoint. In this example we use it to make data available to select2.
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.
In this example we use the function register_rest_route to add the route /search to the WP REST API under the
A tutorial on how to create a custom profile edit page
Just looking for the how-to? Scroll down to ‘How-to starts below’. One on the benefits of WordPress is it’s collection of over 40.000 plugins in the plugin repository. There is probably a plugin available for every feature you can imagine, just as long the feature is not too site specific.
However, as a professional WordPress developer I try to use as little plugins as possible. Therefor I have multiple reasons:
I want to know exacly what code is in my project.
When a error accurs in a plugin you have to rely on someone else to fix a bug.
A plugin almost always has more features than you need.
You have to rely on someone else to release a bugfix.
Less code = better!
Just recently a project I was working on needed a page where users could edit their profile. The website was a internal website for a hospital where specialists and doctors could log-in and find info on different medical publications. And as said, the doctors had te be able to edit their profile by themself.
This was a perfect use case where a plug-in could add this feature to the website. However in my opinion that was just overkill as the only thing needed was a simple form with some logic basicly :).