Blog

  • Form Handling in Headless WordPress: Three Approaches

    Forms are everywhere – contact forms, newsletter signups, lead capture. Here’s how to handle them in a headless WordPress setup.

    Approach 1: WordPress Forms (Recommended)

    Use Contact Form 7 or WPForms in WordPress, submit from Next.js:

    // Server action in Next.js
    async function handleSubmit(formData: FormData) {
      const response = await fetch(
        'https://cms.flatwp.com/wp-json/contact-form-7/v1/contact-forms/123/feedback',
        {
          method: 'POST',
          body: formData,
        }
      );
      
      return response.json();
    }

    Pros:

    • Editors manage form fields in WordPress
    • Email notifications handled by WordPress
    • Submissions stored in WordPress database
    • Works with existing WordPress plugins

    Cons:

    • Extra API call to WordPress
    • WordPress becomes a dependency for forms

    Approach 2: Next.js Server Actions

    Handle forms entirely in Next.js with server actions:

    // app/contact/actions.ts
    'use server'
    
    export async function submitContact(formData: FormData) {
      const data = {
        name: formData.get('name'),
        email: formData.get('email'),
        message: formData.get('message'),
      };
      
      // Send email via Resend, SendGrid, etc.
      await sendEmail(data);
      
      // Store in database if needed
      await db.contacts.create(data);
    }

    Pros:

    • No WordPress dependency
    • Full control over validation and processing
    • Modern server actions pattern
    • Can integrate with any email service

    Cons:

    • Editors can’t manage form fields
    • Need separate storage for submissions
    • More code to maintain

    Approach 3: Third-Party Services

    Use Formspree, Tally, or similar:

    <form action="https://formspree.io/f/your-id" method="POST">
      <input name="email" type="email" />
      <button type="submit">Submit</button>
    </form>

    Pros:

    • Zero backend code
    • Spam protection included
    • Email notifications handled
    • Nice dashboard for submissions

    Cons:

    • Monthly cost ($0-$20)
    • Less customization
    • Another service to manage

    FlatWP’s Approach

    We include adapters for all three approaches. Our default recommendation:

    • Use WordPress forms for marketing sites (editors need control)
    • Use server actions for apps (more dynamic needs)
    • Use third-party for MVPs (fastest to ship)

    The beauty of headless is you’re not locked in. Start with one, switch to another as needs change.

    Validation with Zod

    Regardless of approach, validate with Zod:

    const contactSchema = z.object({
      name: z.string().min(2),
      email: z.string().email(),
      message: z.string().min(10),
    });
    
    export async function submitContact(formData: FormData) {
      const data = contactSchema.parse({
        name: formData.get('name'),
        email: formData.get('email'),
        message: formData.get('message'),
      });
      
      // data is now typed and validated
    }

    Type-safe forms with runtime validation. Beautiful.

  • The Cost of Going Headless: Is It Worth It?

    Headless WordPress adds complexity. Let’s be honest about the costs and benefits so you can make an informed decision.

    The Real Costs

    Development Time: A headless setup takes longer initially. Traditional WordPress theme: 1-2 weeks. Headless setup: 2-4 weeks for your first project. (FlatWP aims to cut this to 1 week.)

    Hosting:

    • WordPress: $15-50/month (same as before)
    • Next.js: $0-20/month (Vercel free tier or $20/month Pro)
    • Total: Similar to traditional WordPress

    Maintenance: Two systems to update instead of one. But Next.js updates are painless compared to WordPress plugin/theme updates.

    Learning Curve: Your team needs to know React and Next.js. Not trivial for PHP-only developers.

    The Benefits

    Performance: 3-5x faster page loads translate to higher conversion rates. For e-commerce, a 100ms improvement = 1% revenue increase.

    Security: WordPress admin is hidden behind authentication. Your public site has no PHP, no wp-admin exposure, no plugin vulnerabilities.

    Developer Experience: Modern tooling, TypeScript, component libraries, hot reload. This makes development faster and more enjoyable.

    Scaling: Static/ISR pages handle traffic spikes effortlessly. No server scaling needed.

    Flexibility: Want to add a React dashboard? Native mobile app? Share your WordPress content across multiple frontends easily.

    When Traditional WordPress Makes Sense

    Don’t go headless if:

    • You need to ship in <1 week
    • Budget is extremely tight
    • Your team doesn’t know React
    • The site is simple (5 pages, basic blog)
    • You rely heavily on WordPress themes

    When Headless Makes Sense

    Go headless if:

    • Performance matters (e-commerce, high-traffic)
    • You want modern DX
    • Security is critical
    • You need flexibility for future features
    • Your team knows or wants to learn React

    The FlatWP Advantage

    FlatWP reduces the “time to value” dramatically. Instead of 2-4 weeks for your first headless site, you’re shipping in 1 week with production-ready patterns.

    That cost reduction makes headless viable for many more projects.

    ROI Calculation

    Let’s say you’re an agency building client sites:

    • Traditional WordPress: $8k project, 2 weeks = $4k/week
    • Custom Headless: $12k project, 3 weeks = $4k/week (same rate, longer timeline)
    • FlatWP Headless: $10k project, 1.5 weeks = $6.6k/week (higher margins)

    The efficiency gain pays for itself immediately.

  • WooCommerce Headless: Coming to FlatWP Pro

    E-commerce is the #1 requested feature for FlatWP. We’re building a production-ready headless WooCommerce storefront for the Pro version.

    Why Headless WooCommerce?

    Traditional WooCommerce sites are slow. Product pages take 3-5 seconds to load, cart updates require full page refreshes, and checkout flows are clunky.

    A headless storefront gives you:

    • Instant navigation: Products load in <500ms
    • Smooth cart updates: Add to cart without page reload
    • Modern checkout: One-page, optimized flows
    • Better mobile UX: App-like experience

    Speed directly impacts conversion. Amazon found 100ms slower = 1% sales drop. For a $1M/year store, that’s $10k annually.

    The Technical Approach

    We’re using WPGraphQL for WooCommerce to query products, categories, and orders:

    query GetProducts {
      products(first: 20) {
        nodes {
          id
          name
          slug
          price
          image {
            url
          }
          ... on SimpleProduct {
            stockQuantity
          }
        }
      }
    }

    For cart and checkout, we’re using WooCommerce’s REST API with JWT authentication.

    What’s Included

    FlatWP Pro’s WooCommerce integration will include:

    • Product catalog: Grid/list views with filtering and sorting
    • Single product pages: Gallery, variants, add to cart
    • Cart: Persistent cart with quantity updates
    • Checkout: One-page checkout with Stripe integration
    • Account pages: Order history, address management
    • Search: Product search with filters

    All built with Shadcn components, fully typed with TypeScript.

    Performance Targets

    We’re targeting:

    • Product list: <1s LCP
    • Product detail: <1.5s LCP
    • Add to cart: <200ms
    • Checkout page: <2s LCP

    These are 3-5x faster than typical WooCommerce sites.

    Launch Timeline

    We’re targeting Q1 2025 for the WooCommerce beta. It’ll be included with FlatWP Pro ($299 one-time or $99/year).

    If you’re interested in early access, join our waitlist.

    Why Not Free?

    E-commerce is complex. We’re investing significant engineering time to make it production-ready:

    • Proper cart state management
    • Payment gateway integration
    • Tax calculations
    • Shipping methods
    • Order processing

    This is Pro-tier functionality. The revenue funds ongoing development and support.

    For DIY developers, we’ll document how to build WooCommerce integration yourself using the free version.

  • Monorepo Architecture for FlatWP Projects

    FlatWP uses a monorepo to keep Next.js and WordPress plugin development in sync. Here’s why and how it works.

    Why Monorepo?

    In a headless setup, you’re maintaining:

    • Next.js frontend
    • WordPress plugin for webhooks/admin
    • Shared TypeScript types
    • Configuration files

    Keeping these in separate repos means:

    • Types get out of sync
    • Changes require coordinating multiple PRs
    • Testing becomes complicated
    • New developers need to clone multiple repos

    A monorepo solves all of this.

    Our Structure

    flatwp/
    ├── apps/
    │   ├── web/           # Next.js app
    │   └── wp-plugin/     # WordPress plugin
    ├── packages/
    │   ├── types/         # Shared TS types
    │   └── config/        # ESLint, TS configs
    ├── package.json
    └── pnpm-workspace.yaml

    Shared Types in Action

    When you generate GraphQL types, both the Next.js app and WordPress plugin admin UI access them:

    // packages/types/src/wordpress.ts
    export interface Post {
      id: string;
      title: string;
      slug: string;
    }
    
    // Used in apps/web
    import { Post } from '@flatwp/types';
    
    // Used in apps/wp-plugin admin UI
    import { Post } from '@flatwp/types';

    One source of truth, no duplication.

    pnpm Workspaces

    We use pnpm for fast, efficient dependency management:

    # pnpm-workspace.yaml
    packages:
      - 'apps/*'
      - 'packages/*'

    Run commands across all workspaces:

    pnpm dev          # Start all apps in dev mode
    pnpm build        # Build all apps
    pnpm type-check   # Type check everything

    Turborepo for Speed

    Turborepo caches builds and runs tasks in parallel:

    // turbo.json
    {
      "tasks": {
        "build": {
          "dependsOn": ["^build"],
          "outputs": [".next/**", "build/**"]
        },
        "dev": {
          "cache": false,
          "persistent": true
        }
      }
    }

    Second builds are near-instant thanks to caching.

    Benefits We’ve Seen

    • Faster onboarding: One clone, one install
    • Atomic changes: Update types + usage in one commit
    • Better CI: Test everything together
    • Shared tooling: One ESLint config, one Prettier config

    When NOT to Monorepo

    If you’re just starting and want to move fast, skip the monorepo initially. Build the Next.js app first, add the WordPress plugin later.

    But once you’re serious about shipping, the monorepo pays dividends.

  • FlatWP 2025: Product Roadmap and Vision

    We’re launching FlatWP’s open-source version in December 2024. Here’s what we’re planning for 2025.

    Q1 2025: Foundation (Free Version)

    January – Public Beta

    • Open-source release on GitHub
    • Core ISR implementation
    • Basic WordPress plugin
    • Preview mode
    • Image optimization
    • Comprehensive documentation

    February – Community Building

    • Launch on Product Hunt
    • Create video tutorials
    • Start Discord community
    • Collect feedback and iterate

    March – Stability & DX

    • Bug fixes from beta feedback
    • Improved error messages
    • Better CLI tooling
    • VSCode extension (maybe)

    Q2 2025: Pro Features

    April – Pro Launch

    • Advanced ACF component library (20+ blocks)
    • Enhanced WordPress plugin with React admin UI
    • Performance monitoring dashboard
    • Priority support for Pro users
    • Commercial license

    May – WooCommerce Beta

    • Headless storefront (beta)
    • Product catalog with filtering
    • Cart functionality
    • Basic checkout flow

    June – Multi-Language

    • WPML/Polylang integration
    • i18n routing in Next.js
    • Language switcher component
    • Translated URLs

    Q3 2025: Scale & Polish

    July – WooCommerce GA

    • Production-ready e-commerce
    • Multiple payment gateways
    • Order management
    • Customer accounts

    August – Agency Features

    • White-label options
    • Multi-site support
    • Client handoff templates
    • Agency license tier

    September – Performance Tools

    • Advanced caching strategies
    • CDN optimization guides
    • Performance budgets
    • Automated Lighthouse CI

    Q4 2025: Enterprise & Ecosystem

    October – Template Marketplace

    • Industry-specific templates
    • Premium themes
    • Pre-built pages
    • Community contributions

    November – Enterprise Features

    • Advanced security options
    • Custom deployment workflows
    • Dedicated support
    • SLA agreements

    December – Year in Review

    • Retrospective blog post
    • Community awards
    • 2026 planning

    Community-Driven Development

    This roadmap will evolve based on user feedback. We’re building FlatWP with the community, not just for it.

    Top-voted GitHub issues get priority. Feature requests in Discord get considered. If you have ideas, we’re listening.

    Pricing Reminder

    • Free: Always free, MIT license
    • Pro: $299 one-time or $99/year (launching April 2025)
    • Agency: $999/year (launching August 2025)

    Early adopters who buy Pro before June 2025 get lifetime updates for their one-time payment.

    Let’s build the future of WordPress together.