📡 WordPress REST API = Headless Power
WordPress is great for content. REST API makes it headless. Any frontend, any platform.
📝 REST API Endpoints
# Base URL https://yourdomain.com/wp-json/wp/v2/ # Posts GET /posts # Get posts GET /posts/123 # Get specific post POST /posts # Create post PUT /posts/123 # Update post DELETE /posts/123 # Delete post # Pages GET /pages GET /pages/123 # Categories GET /categories GET /categories/5 # Tags GET /tags GET /tags/10 # Users GET /users GET /users/1 # Custom Post Types GET /my-cpt GET /my-cpt/123 # ACF Fields GET /acf/v3/posts/123 # Authentication # JWT Auth plugin # OAuth 2.0 # Application Passwords
🎯 Using REST API
// Fetch posts with JavaScript
async function getPosts() {
const response = await fetch('https://yourdomain.com/wp-json/wp/v2/posts?per_page=10');
const posts = await response.json();
return posts;
}
// Get post with featured image
async function getPostWithImage(id) {
const post = await fetch(`https://yourdomain.com/wp-json/wp/v2/posts/${id}?_embed`)
.then(r => r.json());
const featuredImage = post._embedded['wp:featuredmedia'][0];
return { post, image: featuredImage };
}
// Create post
async function createPost(title, content) {
const response = await fetch('https://yourdomain.com/wp-json/wp/v2/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_TOKEN'
},
body: JSON.stringify({
title: title,
content: content,
status: 'publish'
})
});
return response.json();
}
// React component
function WordPressPosts() {
const [posts, setPosts] = useState([]);
useEffect(() => {
fetch('https://yourdomain.com/wp-json/wp/v2/posts?per_page=5')
.then(r => r.json())
.then(data => setPosts(data));
}, []);
return (
<div>
{posts.map(post => (
<article key={post.id}>
<h2 dangerouslySetInnerHTML={{__html: post.title.rendered}} />
<div dangerouslySetInnerHTML={{__html: post.excerpt.rendered}} />
</article>
))}
</div>
);
}
💡 Headless WordPress Tips
- Use plugins: Advanced Custom Fields (ACF), Custom Post Types
- Secure with JWT Authentication
- Cache API responses
- Use GraphQL for complex queries
- Consider static site generation
“WordPress REST API enables headless CMS. Any frontend, any platform. Essential for modern web.”
