Experience the powerful AI writing right inside WordPress
Show stunning before-and-after transformations with image sliders.
Improve user engagement by showing estimated reading time.
Written by Tasfia Chowdhury Supty
Showcase Designs Using Before After Slider.
WordPress has evolved beyond a traditional content management system (CMS) into a powerful headless CMS, thanks to its REST API. This allows developers to build custom themes, decouple the front-end from the back-end, and create seamless integrations with external applications.
If you’re looking to develop a WordPress theme with REST API development, this guide will cover everything from the types of themes to development steps, best practices, and FAQs.
The WordPress REST API provides a JSON-based interface, making it possible to:
✅ Develop Headless WordPress Websites – Use WordPress as a content backend and build a front-end with React, Vue.js, or Angular.✅ Enhance Performance – Fetch only the necessary data instead of loading full-page templates.✅ Enable Cross-Platform Integration – Connect WordPress with mobile apps, third-party services, and external databases.✅ Improve Customization – Fetch dynamic content without relying on traditional PHP-based themes.
Before diving into WordPress theme development with REST API, let’s explore the different types of themes that leverage API integration.
A headless theme completely separates the front-end from WordPress, using JavaScript frameworks for rendering.
Technologies Used:
Example Use Case: A fast and dynamic blog where the front-end pulls content from the WordPress REST API.
Hybrid themes mix traditional WordPress PHP templates with REST API calls for dynamic content fetching.
Key Features:
Example Use Case: A WooCommerce store where the product listing is fetched via REST API for faster loading.
PWA themes offer app-like experiences while remaining accessible via a browser.
Example Use Case: A news website with offline reading capability.
SPA themes dynamically update the page content without reloading the entire page.
Example Use Case: A portfolio website where clicking on a project loads new content dynamically.
Install WordPress locally or on a development server and ensure the REST API is enabled.
Check REST API Functionality:Visit:
https://yourwebsite.com/wp-json/wp/v2/posts
If you see JSON data, the API is working.
Navigate to /wp-content/themes/ and create a new folder:
/wp-content/themes/
my-restapi-theme
Inside this folder, create style.css and index.php:
style.css
index.php
style.css (Theme Header):
/* Theme Name: My REST API Theme Author: Your Name Description: A WordPress theme using REST API. Version: 1.0 */
index.php:
<?php get_header(); echo '<div id="app">Loading...</div>'; get_footer(); ?>
Instead of using WP_Query, fetch posts dynamically via REST API in JavaScript.
WP_Query
Add the following script to index.php:
<script> document.addEventListener("DOMContentLoaded", function () { fetch('https://yourwebsite.com/wp-json/wp/v2/posts') .then(response => response.json()) .then(posts => { let output = ''; posts.forEach(post => { output += `<h2>${post.title.rendered}</h2><p>${post.excerpt.rendered}</p>`; }); document.getElementById('app').innerHTML = output; }) .catch(error => console.log('Error fetching data:', error)); }); </script>
This script loads posts dynamically without requiring a full page reload.
If you need custom data, create a REST API endpoint in functions.php:
functions.php
function custom_rest_endpoint() { register_rest_route('mytheme/v1', '/custom-data/', array( 'methods' => 'GET', 'callback' => 'custom_data_callback', )); } function custom_data_callback() { return array( 'site_name' => get_bloginfo('name'), 'theme' => wp_get_theme()->get('Name') ); } add_action('rest_api_init', 'custom_rest_endpoint');
Now, visit:
https://yourwebsite.com/wp-json/mytheme/v1/custom-data
You’ll see a JSON response with site info.
If users need to post data via API, authentication is required. Use JWT Authentication:
.htaccess
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
curl -X POST -H "Authorization: Bearer YOUR_TOKEN" https://yourwebsite.com/wp-json/wp/v2/posts
✔ Use Caching – Avoid excessive API calls by caching responses.✔ Minimize API Requests – Only fetch the necessary data.✔ Secure API Endpoints – Protect sensitive data with authentication.✔ Optimize for Performance – Load data asynchronously for faster page speeds.✔ Ensure Mobile Responsiveness – REST API-powered themes should work on all devices.
The WordPress REST API allows developers to interact with WordPress using JSON data, enabling integration with external apps, mobile applications, and front-end frameworks.
Yes! You can create a headless WordPress site using React, Vue.js, or Angular, where the front-end is entirely separate from WordPress.
GraphQL is more efficient for fetching nested data, while REST API is more widely supported. The choice depends on your project requirements.
Use JWT Authentication or OAuth plugins to secure API requests.
Developing a WordPress theme with REST API allows for greater flexibility, speed, and integration possibilities. Whether you’re building a headless WordPress site, a hybrid theme, or a PWA, the REST API empowers developers to create dynamic and high-performance themes.
By following this guide, you’re now equipped to develop a modern WordPress theme with REST API and take advantage of the power of decoupled architectures. 🚀
This page was last edited on 20 February 2025, at 5:52 pm
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
How many people work in your company?Less than 1010-5050-250250+
By proceeding, you agree to our Privacy Policy