What is an Endpoint and how does it work in WordPress?
An Endpoint in WordPress is a URL that allows us to customize and expand the capability of the API. It acts as a custom access point for interacting with site data. This means that you can define specific paths to retrieve information tailored to your needs. WordPress provides us with a specific function to generate these access points, called register_rest_route
.
How do we use the register_rest_route function?
The register_rest_route
function is essential to create your own Endpoints. This function receives three main parameters:
- Namespace: this parameter will override the
wp/v2
URI by default in WordPress. It will act as your workspace for all custom routes.
- Route: In this, you define the additional parameters you want to ask for in your custom URL.
- Callback Function: This is the function that will process the request and return the requested data.
There is a fourth optional parameter, called override
. It allows us to specify if we want this function to override other functions with the same path.
How to create an Endpoint in WordPress?
To create a custom Endpoint that calls news on the main page, follow these steps:
-
Define a new function in your functions.php
file. Call this function, for example, news_api
.
-
Inside define the function register_rest_route
specifying:
function news_api() { register_rest_route('pg/v1', '/news/(?P<quantity>\d+)', array( ' methods' => 'GET', ' callback' => 'news_order' ));} }
- Use
pg/v1
as namespace.
- Define the path you want to call (in this case,
/news
) by adding a dynamic attribute with a regular expression /d+
to indicate that it will be a number.
-
Create the callback function, news_order
, which receives the data and processes it.
function news_order($data) { $quantity = $data['quantity']; }
-
Finally, register this route in the WordPress interface using an add_action
.
add_action('rest_api_init', 'novelties_api');
How to adapt an AJAX to an Endpoint?
Implementing an Endpoint in WordPress is similar to AJAX requests, but with REST API it works more efficiently.
- Custom data: Unlike AJAX which can rely on
admin-ajax.php
, with REST API the responses are more structured.
- Structured responses: Return your data in JSON format, which is ideal for front-end applications.
Practical tips for using Endpoints in WordPress
- Optimize your queries: Always pass only the amount of data you need.
- Security first: Validate and sanitize user input before processing it.
- Version your namespaces: This way you keep your API clean and organized as you evolve and change routes.
With these tips, you'll be on a solid path to maximizing the potential of your WordPress Endpoints. Be sure to explore more about REST API, as it offers a vast field to innovate and customize your WordPress site. Keep learning and improving your skills to build rich and custom web applications using WordPress as a platform, and success in your learning journey!
Want to see more contributions, questions and answers from the community?