LESSON 1 ⏱️ 10 min read

Introduction to the WordPress REST API

What is the REST API?

The WordPress REST API provides a standardized way to interact with WordPress using HTTP requests. Instead of loading PHP pages, you send requests to URLs (endpoints) and receive JSON data in response.

REST stands for REpresentational State Transfer – an architectural style for building web services.

Why Use the REST API?

Use CaseExample
Headless WordPressReact/Vue frontend with WordPress backend
Mobile AppsiOS/Android apps fetching WordPress content
External IntegrationsSyncing with CRMs, email services
JavaScript InterfacesReal-time admin dashboards
Multi-site DataAggregating content from multiple WordPress sites

The API Structure

WordPress REST API endpoints follow this pattern:

https://yoursite.com/wp-json/wp/v2/posts
        └── base β”€β”€β”˜β””namespaceβ”˜β””routeβ”˜

Namespace: Groups related endpoints (wp/v2 is core WordPress) Route: The specific resource (posts, pages, users)

Making Your First Request

Open your browser or use curl:

# Get all posts
curl https://yoursite.com/wp-json/wp/v2/posts

# Get a specific post
curl https://yoursite.com/wp-json/wp/v2/posts/123

# Get posts with parameters
curl "https://yoursite.com/wp-json/wp/v2/posts?per_page=5&orderby=date"

Using JavaScript

// Fetch posts with the Fetch API
async function getPosts() {
    const response = await fetch('/wp-json/wp/v2/posts');
    const posts = await response.json();
    
    posts.forEach(post => {
        console.log(post.title.rendered);
    });
}

getPosts();

Core Endpoints

WordPress provides these built-in endpoints:

EndpointDescription
/wp/v2/postsBlog posts
/wp/v2/pagesStatic pages
/wp/v2/mediaAttachments/images
/wp/v2/categoriesPost categories
/wp/v2/tagsPost tags
/wp/v2/usersUser accounts
/wp/v2/commentsPost comments
/wp/v2/settingsSite settings (authenticated)

Discovering the API

The API is self-documenting. Visit the index:

curl https://yoursite.com/wp-json/

This returns all available namespaces and routes with their supported methods and arguments.

Tip: Install a browser extension like "JSON Formatter" to view API responses more clearly.

HTTP Methods

REST APIs use HTTP methods to indicate actions:

MethodActionExample
GETRead dataFetch posts
POSTCreate newAdd a new post
PUT/PATCHUpdateEdit a post
DELETERemoveDelete a post
# GET - Read
curl -X GET /wp-json/wp/v2/posts

# POST - Create (requires authentication)
curl -X POST /wp-json/wp/v2/posts 
    -H "Authorization: Basic BASE64_CREDENTIALS" 
    -d "title=New Post&content=Hello World&status=publish"

# DELETE - Remove
curl -X DELETE /wp-json/wp/v2/posts/123 
    -H "Authorization: Basic BASE64_CREDENTIALS"

Response Structure

API responses include helpful metadata in headers:

X-WP-Total: 42           # Total items available
X-WP-TotalPages: 5       # Total pages for pagination
Link: <...>; rel="next"  # Pagination links

The JSON body contains the actual data:

{
    "id": 123,
    "date": "2026-01-15T10:30:00",
    "title": {
        "rendered": "My Post Title"
    },
    "content": {
        "rendered": "<p>Post content here...</p>"
    },
    "excerpt": {
        "rendered": "<p>Short excerpt...</p>"
    },
    "_links": {
        "self": [{"href": "..."}],
        "author": [{"href": "..."}]
    }
}

Enabling/Disabling the API

The REST API is enabled by default. To restrict it:

// Require authentication for all REST requests
add_filter( 'rest_authentication_errors', function( $result ) {
    if ( true === $result || is_wp_error( $result ) ) {
        return $result;
    }
    
    if ( ! is_user_logged_in() ) {
        return new WP_Error(
            'rest_not_logged_in',
            'You must be logged in to access the API.',
            array( 'status' => 401 )
        );
    }
    
    return $result;
});

Next Steps

In the next lesson, we'll explore querying data in depth – filtering, pagination, embedding related data, and field selection.

🎯 Lesson Complete! You understand REST API fundamentals and can make basic requests to WordPress endpoints.