0
0
WordpressHow-ToBeginner · 4 min read

Create Custom REST API Endpoint in WordPress Easily

To create a custom REST API endpoint in WordPress, use the register_rest_route function inside the rest_api_init action hook. Define your endpoint path, HTTP methods, and a callback function that returns the data you want to expose.
📐

Syntax

The register_rest_route function registers a new REST API route in WordPress. It requires three main parts:

  • Namespace: A string to group your routes, e.g., myplugin/v1.
  • Route: The endpoint path, e.g., /hello.
  • Arguments: An array defining HTTP methods and the callback function.

This function must be called inside the rest_api_init action to ensure WordPress loads the REST API first.

php
add_action('rest_api_init', function () {
    register_rest_route('namespace/v1', '/route', [
        'methods' => 'GET',
        'callback' => 'your_callback_function',
    ]);
});
💻

Example

This example creates a custom endpoint /wp-json/myplugin/v1/hello that returns a simple greeting message in JSON format.

php
<?php
add_action('rest_api_init', function () {
    register_rest_route('myplugin/v1', '/hello', [
        'methods' => 'GET',
        'callback' => function () {
            return ['message' => 'Hello, this is a custom REST API endpoint!'];
        },
    ]);
});
Output
{"message":"Hello, this is a custom REST API endpoint!"}
⚠️

Common Pitfalls

Common mistakes when creating custom REST API endpoints include:

  • Not hooking register_rest_route inside rest_api_init, causing the endpoint not to register.
  • Using incorrect HTTP methods or forgetting to specify them.
  • Callback functions not returning data properly or returning non-serializable data.
  • Not handling permissions, which can expose sensitive data unintentionally.

Always test your endpoint URL and check for errors in the browser or API client.

php
<?php
// Wrong: register_rest_route called outside rest_api_init
// This will not work as expected
// register_rest_route('myplugin/v1', '/bad', [
//     'methods' => 'GET',
//     'callback' => function () {
//         return 'This will not work';
//     },
// ]);

// Right: register_rest_route inside rest_api_init
add_action('rest_api_init', function () {
    register_rest_route('myplugin/v1', '/good', [
        'methods' => 'GET',
        'callback' => function () {
            return ['success' => true];
        },
    ]);
});
📊

Quick Reference

Summary tips for creating custom REST API endpoints in WordPress:

  • Always use rest_api_init action to register routes.
  • Define a clear namespace and route path.
  • Specify HTTP methods like GET, POST, etc.
  • Write callback functions that return data in arrays or objects serializable to JSON.
  • Consider adding permission callbacks for security.

Key Takeaways

Use register_rest_route inside rest_api_init to create custom endpoints.
Define namespace, route, HTTP methods, and a callback function clearly.
Return data in a format that WordPress can convert to JSON.
Test your endpoint URL to ensure it works as expected.
Add permission checks to protect sensitive data.