WordPress Create custom REST endpoint
Last Updated :
10 May, 2024
WordPress, as a powerful content management system (CMS), allows developers to create custom REST endpoints. These endpoints enable communication between external applications and your WordPress site, making it possible to retrieve or manipulate data programmatically. WordPress gives various functions to work on REST API. Let's take a look at how you can easily create your custom endpoint.
In this article, we will see how to create a custom REST endpoint in WordPress. WordPress is not only used for blogging and simple site platforms. Now it's used for large-scale enterprise projects and even a headless CMS. So if you are using WordPress as a headless CMS, you must know how to work with the REST APIs.
We will use a rest_api_init action hook, and register_rest_route() inbuild function to create our custom endpoint.
Steps to Create a custom REST endpoint
You need to add this code to the theme's functions.php file.
Step 1: Create a function that registers the REST route with the register_rest_route() function.
PHP
<?php
function create_custon_endpoint(){
register_rest_route(
'wp/v2',
'/custom-ep',
array(
'methods' => 'GET',
'callback' => 'get_response',
)
);
}
?>
This function's first parameter is namespace 'wp/v2', the second parameter is endpoint name '/custom-ep' and the third parameter is an array where you can add methods like GET, POST, DELETE, etc., and a callback function. You can customize these values according to your need.
For more details, you can check the document.
Step 2: Create callback function get_response().
PHP
<?php
function get_response() {
// Your code...
return 'This is your data!';
}
?>
In this function, you can do whatever you want to add to your endpoint.
Step 3: Add action hook rest_api_init which will run our function create_custon_endpoint on initialization of REST API on WordPress.
PHP
<?php
add_action( 'rest_api_init', 'create_custon_endpoint');
?>
Complete Code: Below given the whole steps in one frame.
PHP
/* Create Custom Endpoint */
add_action( 'rest_api_init', 'create_custon_endpoint' );
function create_custon_endpoint(){
register_rest_route(
'wp/v2',
'/custom-ep',
array(
'methods' => 'GET',
'callback' => 'get_response',
)
);
}
function get_response() {
// your code
return 'This is your data!';
}
Output:
endpoint's response
Similar Reads
WordPress Register new REST field WordPress provides a powerful REST API that allows developers to interact with data in a structured way. By default, WordPress includes essential data in the API responses, but sometimes you might need to add custom fields or third-party integration data. Registering a new rest field in your API res
3 min read
CRUD Operation in REST API using PHP A REST (Representational State Transfer) API allows communication between a client and a server through HTTP requests. PHP, a widely used server-side scripting language, is well-suited for creating REST APIs due to its simplicity and rich ecosystem. This article provides a step-by-step guide on buil
5 min read
How to Test API with REST Assured? REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, easy-to-maintain tests for RESTful APIs. It allows you to specify the expectations for HTTP responses from a RESTful API, and it integrates seamlessly with JUnit, the most popular testing framework fo
5 min read
REST API Testing and Manual Test Cases REST is a set of architectural styles that acts as an interface between physically separate components across the internet. In simple language, one can say that this allows the requesting system to access web resources by using a uniform and predefined set of rules. It is built on a client-server pa
11 min read
Building a REST API with PHP and MySQL This brief tutorial is a step-by-step guide on how to develop a REST API using PHP and MySQL. REST API will implement HTTP commands (Get, Post, Put, DELETE) and response will be in form of JSON. For development setup, we will be using the XAMPP while for testing of the API, we will use the Postman a
5 min read