We continue exploring the topic of custom endpoints with API key access. Today we'll look at an example of saving data to the database and validating the key before saving.

Order website or plugin development for WordPress, website development on the Laravel, Symfony, or Yii2 framework…

We continue exploring the topic of custom endpoints with API key access. Today we'll look at an example of saving data to the database and validating the key before saving.

I decided to slightly complicate our previous code by adding a simple API KEY access check. The example below demonstrates the most basic implementation of access control via a key. In real applications, it is recommended to use something more advanced, like access validation via a Bearer token.

One of the WordPress hooks where we can perform a redirect is template_redirect. In it, we’ll place our check and perform a redirect to the homepage.

To install the jQuery gallery (popup window) script Fancybox, run the following command in the console:
bower i fancybox
But before installing the library, make sure that Node.js and npm are installed on your system. You can read how to do this in this article.

As mentioned in previous articles, WP has a rich set of built-in features for working with its API.
Today, we’ll look at how to create a custom endpoint to read data.
To implement a basic API in WordPress, we’ll need the action hook rest_api_init and the function register_rest_route.