WordPress Create custom REST endpoint Last Updated : 10 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 endpointYou 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 Comment More infoAdvertise with us Next Article WordPress Create custom REST endpoint K krupalpanchal Follow Improve Article Tags : PHP Technical Scripter 2022 Similar Reads AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an 4 min read Python Match Case Statement Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement 7 min read PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages. It allows developers to handle database interactions, session management, and form handling tasks.PHP code is execute 9 min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read RJ45 Color Code Pre-requisites: RJ Full Form RJ45 is a well-known ethernet connectivity connector that allows users to connect through wired internet. there are other ports also which do the same, but RJ45 is widely used and most common in wired internet connection interfaces. It is an ethernet cable consisting of 3 min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read History of Operating System An operating system is a type of software that acts as an interface between the user and the hardware. It is responsible for handling various critical functions of the computer and utilizing resources very efficiently so the operating system is also known as a resource manager. The operating system 8 min read Introduction to Monotonic Stack - Data Structure and Algorithm Tutorials A monotonic stack is a special data structure used in algorithmic problem-solving. Monotonic Stack maintaining elements in either increasing or decreasing order. It is commonly used to efficiently solve problems such as finding the next greater or smaller element in an array etc.Monotonic StackTable 12 min read What is Fragmentation in Operating System? The process of dividing a computer file, such as a data file or an executable program file, into fragments that are stored in different parts of a computer's storage medium, such as its hard disc or RAM, is known as fragmentation in computing. When a file is fragmented, it is stored on the storage m 8 min read Single Layer Perceptron in TensorFlow Single Layer Perceptron is inspired by biological neurons and their ability to process information. To understand the SLP we first need to break down the workings of a single artificial neuron which is the fundamental building block of neural networks. An artificial neuron is a simplified computatio 4 min read Like