Creating Custom REST API Endpoints in WordPress

SeanAUS120 - Sep 9 - - Dev Community

Creating Custom REST API Endpoints in WordPress

WordPress comes with a powerful REST API that allows you to interact with your site programmatically. However, sometimes you might need to extend this functionality by creating custom API endpoints tailored to your needs.

In this post, I’ll walk you through how to create a custom REST API endpoint in WordPress using PHP. I was recently mucking around with this on World First Aid Day as I needed to get some remote content added from a non-Wordpress source.

Check this out:

Step 1: Register the Custom Endpoint

To add a custom REST API endpoint, you need to hook into the rest_api_init action and use the register_rest_route function. Here’s how:

function register_custom_api_endpoint() {
    register_rest_route('custom/v1', '/data/', array(
        'methods' => 'GET',
        'callback' => 'custom_api_endpoint_callback',
    ));
}
add_action('rest_api_init', 'register_custom_api_endpoint');
Enter fullscreen mode Exit fullscreen mode

This code registers a new route (/wp-json/custom/v1/data/) which will respond to GET requests.

Step 2: Create the Callback Function

The callback function handles what data should be returned when the endpoint is hit. Here’s an example that returns a simple JSON object:

function custom_api_endpoint_callback() {
    $response = array(
        'status' => 'success',
        'message' => 'Custom API data retrieved!',
    );

    return rest_ensure_response($response);
}
Enter fullscreen mode Exit fullscreen mode

In this case, the API returns a success status and a custom message. You can expand this function to pull data from the WordPress database or any other source.

Step 3: Test the Custom Endpoint

Now that you’ve registered the endpoint and created a callback, you can test your new custom API by visiting:

https://yourwebsite.com/wp-json/custom/v1/data/
Enter fullscreen mode Exit fullscreen mode

You should see a JSON response similar to this:

{
    "status": "success",
    "message": "Custom API data retrieved!"
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Adding Parameters to the Endpoint

To make your API more dynamic, you can accept parameters from the URL. Let’s modify the endpoint to handle query parameters like this:

function custom_api_endpoint_callback( $data ) {
    $name = ( isset( $data['name'] ) ) ? $data['name'] : 'Guest';

    $response = array(
        'status' => 'success',
        'message' => 'Hello, ' . $name . '!',
    );

    return rest_ensure_response( $response );
}
Enter fullscreen mode Exit fullscreen mode

Now you can visit the endpoint like this:

https://yourwebsite.com/wp-json/custom/v1/data/?name=John
Enter fullscreen mode Exit fullscreen mode

The response will be:

{
"status": "success",
"message": "Hello, John!"
}
Enter fullscreen mode Exit fullscreen mode




Conclusion

By creating custom REST API endpoints in WordPress, you can easily extend your site’s functionality and create custom solutions that interact with external systems. It’s a powerful feature for developers looking to build flexible integrations.

For a demo of this, check out World First Aid Day, a global initiative aimed at raising awareness on life-saving skills.

In use at:

. . . . . . . . . . . . . . . . . . . .
Terabox Video Player