wordpress-node
v1.0.0
Published
IMPORTANT: the REST wordpress API only works if you enable postname in permalinks Minimum version 6.8 with yoast installed and rest API enabled to expose the menu rest api: add add_filter( 'rest_menu_read_access', '\_\_return_true' ); to functions.php (se
Readme
IMPORTANT: the REST wordpress API only works if you enable postname in permalinks Minimum version 6.8 with yoast installed and rest API enabled to expose the menu rest api: add add_filter( 'rest_menu_read_access', '__return_true' ); to functions.php (see https://make.wordpress.org/core/2025/03/27/new-rest-api-filter-for-exposing-menus-publicly-in-wordpress-6-8/)
WordPress Headless Wrapper - Refactored
A refactored WordPress headless wrapper that provides a clean, modular architecture for building headless WordPress applications with Tailwind CSS support.
Running Tests
npm test🔧 Services
WordPressAPIService
Handles all WordPress API interactions:
getContent(slug)- Fetch page/post contentgetPosts(page, perPage)- Fetch paginated postssearchContent(query, page)- Search contentgetMenu(slug)- Fetch menu itemsgetArchive(type, slug, page)- Fetch archive data
TailwindService
Manages Tailwind CSS compilation:
initialize()- Setup Tailwind compilercompileCSS()- Compile CSS from templatesgetCSS()- Get compiled CSSgetStatus()- Get compilation status
TemplateService
Handles template rendering:
renderTemplate(name, data)- Render template with dataloadPartials(html)- Load and insert partialsreplaceTags(html, data)- Replace template tags
CacheService
Manages response caching:
set(key, data)- Cache dataget(key)- Retrieve cached datahas(key)- Check if data existscleanup()- Remove expired entries
🛣️ Route Handlers
The RouteHandlers class contains all route-specific logic:
handlePage()- Handle page/post requestshandleSearch()- Handle search requestshandleArchive()- Handle archive requestshandleTailwindCompile()- Handle CSS compilationhandleAPIRequest()- Handle API requests
🔌 Middleware
The Middleware class provides Express middleware:
setupSecurity()- Helmet security middlewaresetupCORS()- CORS middlewaresetupCache()- Cache middlewaresetupTailwindCSS()- CSS serving middleware
🚀 Usage
import { WordPressHeadlessWrapper } from "./src/wordpress-headless-wrapper";
const wrapper = new WordPressHeadlessWrapper({
wpBaseUrl: "https://your-wordpress-site.com",
templatesDir: "./templates",
port: 3000,
tailwind: {
enabled: true,
configPath: "./tailwind.config.js",
inputPath: "./src/styles/input.css",
outputPath: "./public/styles/main.css",
},
});
wrapper.start();