Merged into core in WordPress 4.7, the WordPress REST API transformed WordPress from a monolithic, server-side PHP publishing platform into an open, universally interoperable Data Provider. By exposing content over standard HTTP requests returning structured JSON (JavaScript Object Notation) payloads, the REST API enables any external software — from a mobile React Native iOS app and a Next.js web application to an IoT device or command-line terminal — to interact with your WordPress database effortlessly.
Figure 39.1: WordPress REST API & Headless Decoupled Architecture
Every WordPress website natively exposes public, read-only REST endpoints under the /wp-json/ namespace:
| HTTP Method | API Endpoint URL Path | Data Returned |
|---|---|---|
GET |
/wp-json/wp/v2/posts |
Retrieves a paginated JSON array of published blog posts with titles, dates, author IDs, and rendered content. |
GET |
/wp-json/wp/v2/pages |
Retrieves published static pages. |
GET |
/wp-json/wp/v2/categories |
Retrieves all taxonomy categories with post counts and slugs. |
GET |
/wp-json/wp/v2/users |
Retrieves public user profiles (authors who have published content). |
When developing custom enterprise applications, developers register custom REST routes using register_rest_route() hooked into the rest_api_init action hook:
// Register Custom Endpoint: GET /wp-json/veewom/v1/latest-courses
add_action('rest_api_init', 'veewom_register_custom_rest_routes');
function veewom_register_custom_rest_routes() {
register_rest_route('veewom/v1', '/latest-courses', array(
'methods' => 'GET', // or WP_REST_Server::READABLE
'callback' => 'veewom_get_latest_courses_api',
'permission_callback' => '__return_true' // Publicly accessible endpoint
));
}
function veewom_get_latest_courses_api($request) {
$args = array(
'post_type' => 'course',
'posts_per_page' => 5,
'post_status' => 'publish'
);
$query = new WP_Query($args);
$results = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$results[] = array(
'id' => get_the_ID(),
'title' => get_the_title(),
'permalink'=> get_the_permalink(),
'fee' => get_field('tuition_fee')
);
}
wp_reset_postdata();
}
// Return clean JSON response with HTTP 200 status
return new WP_REST_Response($results, 200);
}
While GET read requests are public, writing or deleting data (POST, PUT, DELETE) requires authentication:
/wp-json/jwt-auth/v1/token, receives an encrypted JWT bearer token, and includes it in subsequent HTTP Authorization: Bearer <token> headers.In modern WordPress REST development, setting 'permission_callback' is mandatory. Omitting this parameter throws a _doing_it_wrong() security notice. For protected administrative endpoints, always pass a capability verification callback: 'permission_callback' => function() { return current_user_can('edit_posts'); }.
Expand your CMS mastery by studying these tightly integrated WordPress development topics:
Fetch posts, pages, and products to render high-speed React web apps.
Register custom REST routes, controllers, and schema validation.
Authenticate REST requests using Application Passwords and JWT.
Common senior developer interview questions on WP REST API and CORS.