How to Use WordPress REST API: Simple Guide for Beginners
The WordPress REST API lets you interact with your site’s data using HTTP requests like
GET, POST, PUT, and DELETE. You use URLs called endpoints to fetch or change content, often sending or receiving data in JSON format. For example, GET /wp-json/wp/v2/posts fetches posts from your site.Syntax
The WordPress REST API uses HTTP methods to perform actions on resources. The main methods are:
- GET: Retrieve data (like posts or pages).
- POST: Create new data.
- PUT or PATCH: Update existing data.
- DELETE: Remove data.
Requests are made to endpoints under /wp-json/wp/v2/ followed by the resource name, such as posts or pages.
http
GET /wp-json/wp/v2/posts
POST /wp-json/wp/v2/posts
PUT /wp-json/wp/v2/posts/{id}
PATCH /wp-json/wp/v2/posts/{id}
DELETE /wp-json/wp/v2/posts/{id}Example
This example shows how to fetch the latest posts from a WordPress site using JavaScript's fetch API. It sends a GET request to the posts endpoint and logs the titles of the posts.
javascript
async function fetchPosts() { const response = await fetch('https://example.com/wp-json/wp/v2/posts'); if (!response.ok) { console.error('Failed to fetch posts'); return; } const posts = await response.json(); posts.forEach(post => { console.log(post.title.rendered); }); } fetchPosts();
Output
Hello World!
My Second Post
Another Example Post
Common Pitfalls
Common mistakes when using the WordPress REST API include:
- Not enabling pretty permalinks, which can cause endpoints to return 404 errors.
- Trying to modify data without proper authentication, leading to 401 or 403 errors.
- Ignoring the need to send
Content-Type: application/jsonheader when sending JSON data. - Not handling pagination when fetching large lists of items.
Always check your site’s permalink settings and use authentication methods like OAuth or application passwords for write operations.
javascript
/* Wrong: Missing Content-Type header when creating a post */ fetch('https://example.com/wp-json/wp/v2/posts', { method: 'POST', body: JSON.stringify({ title: 'New Post' }) }); /* Right: Include Content-Type header and authentication */ fetch('https://example.com/wp-json/wp/v2/posts', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa('username:application_password') }, body: JSON.stringify({ title: 'New Post' }) });
Quick Reference
Here is a quick summary of common WordPress REST API endpoints and HTTP methods:
| Endpoint | HTTP Method | Purpose |
|---|---|---|
| /wp-json/wp/v2/posts | GET | Get list of posts |
| /wp-json/wp/v2/posts/{id} | GET | Get a single post by ID |
| /wp-json/wp/v2/posts | POST | Create a new post |
| /wp-json/wp/v2/posts/{id} | PUT/PATCH | Update a post |
| /wp-json/wp/v2/posts/{id} | DELETE | Delete a post |
Key Takeaways
Use HTTP methods GET, POST, PUT, DELETE to interact with WordPress data via REST API endpoints.
Always send and receive data in JSON format and set appropriate headers.
Enable pretty permalinks and use authentication for modifying data.
Use the /wp-json/wp/v2/ base URL followed by resource names like posts or pages.
Handle pagination and errors to build reliable API clients.