WPDev.one
Headless
PHP
Backend Data Fetching

Register Custom REST API Endpoint

Create a tailored API route (e.g., /wp-json/my-site/v1/latest) to fetch specific data efficiently, reducing payload size for Headless or JS-heavy sites.

api.php

API Response Simulator

GET
https://api.yoursite.com/wp-json/my-site/v1/latest-posts
Status: 200 OK Time: 45ms Size: 1.24KB
[
"ID": 102,
"post_title": "Headless WordPress Guide",
"post_date": "2024-10-24 10:00:00",
"post_author": 1
,
"ID": 98,
"post_title": "API Authentication 101",
"post_date": "2024-10-20 09:30:00",
"post_author": 2
]

Integration Steps

Add to Plugin or Theme

Place the PHP code in your theme's functions.php or a custom plugin file.

Refresh Permalinks

Go to Settings > Permalinks and click "Save Changes" to flush rewrite rules and register the new route.

Test Endpoint

Visit your-site.com/wp-json/my-site/v1/latest-posts in your browser or Postman to verify.