The WordPress REST API lets you request and send data from WordPress using HTTP endpoints. Instead of loading pages or using AJAX through admin-ajax.php, the REST API gives you direct access to data in JSON format, which works great with JavaScript, mobile apps, React front-ends, and external integrations.
📌 Example: Fetch Latest Posts
fetch('/wp-json/wp/v2/posts?per_page=3')
.then(r => r.json())
.then(data => console.log(data));
What This Does:
- Requests the latest 3 posts from your site.
- The API route is:
/wp-json/wp/v2/posts - The response comes back as JSON.
- You can loop, display, or manipulate the returned data in your front end.
🧠 Key Concepts
| Feature | Explanation |
|---|---|
| REST API Base URL | /wp-json/ |
| Posts Endpoint | /wp-json/wp/v2/posts |
| Response Format | JSON (JavaScript-friendly) |
| Can Filter Data With Query Params | e.g., per_page, search, orderby, etc. |
| Works With External Apps | React, Vue, iOS/Android, Headless WP |
🔑 Key Takeaways
- The REST API outputs JSON, not HTML.
- It allows JavaScript to request data directly from WordPress.
- It is the foundation for Headless WordPress, mobile apps, and SPA front-ends.
- You can access nearly everything: posts, pages, users, menus, custom fields, products.
✅ Example: Display Titles in the Browser
fetch('/wp-json/wp/v2/posts?per_page=3')
.then(res => res.json())
.then(posts => {
posts.forEach(post => {
document.body.innerHTML += `<h2>${post.title.rendered}</h2>`;
});
});
