Skip to content

Add comprehensive headless WordPress and Faust.js support to CF Images plugin#2

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-3b36ac8a-94fe-4388-8c3b-231cdebbfda2
Open

Add comprehensive headless WordPress and Faust.js support to CF Images plugin#2
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-3b36ac8a-94fe-4388-8c3b-231cdebbfda2

Conversation

Copy link

Copilot AI commented Sep 25, 2025

Overview

This PR implements comprehensive support for headless WordPress setups, particularly those using the Faust.js framework. The CF Images plugin can now seamlessly deliver optimized images through Cloudflare in headless environments where WordPress serves as a backend CMS and modern JavaScript frameworks handle the frontend.

Problem Statement

Headless WordPress setups use frameworks like Faust.js, Next.js, and Nuxt.js to build frontends that consume WordPress data via REST API and GraphQL endpoints. However, the CF Images plugin previously blocked image processing during REST API requests, preventing headless sites from benefiting from Cloudflare image optimization.

Solution

Core Integration (app/integrations/class-faust.php)

Added a comprehensive Faust.js integration that:

  • Auto-detects Faust.js plugin activation or allows manual enablement
  • Processes REST API requests for headless environments while maintaining admin functionality
  • Handles complex data structures including ACF fields and nested media objects
  • Supports GraphQL when WPGraphQL is available
  • Provides extensive configuration through the existing CF Images settings interface

Key Features Implemented

REST API Enhancement: The integration intelligently overrides the default REST request blocking to allow image processing for headless setups:

// Before: REST requests were blocked
if ( $this->is_rest_request() ) {
    return false; // No image processing
}

// After: Smart detection for headless environments
$is_rest = apply_filters( 'cf_images_is_rest_request', true, $attachment_id );
// Returns false for headless mode, allowing processing

Content Processing: Automatically processes images within post content delivered via REST API:

// REST API response now includes Cloudflare URLs
{
  "content": {
    "rendered": "<img src=\"https://imagedelivery.net/hash/image-id/w=300\" class=\"wp-image-123\" />"
  }
}

ACF Integration: Enhanced support for Advanced Custom Fields in headless contexts:

// ACF image fields automatically get Cloudflare URLs
$acf_image = [
    'url' => 'https://imagedelivery.net/hash/image-id/public',
    'sizes' => [
        'medium' => 'https://imagedelivery.net/hash/image-id/w=300'
    ]
];

Configuration Options

The integration provides four configurable options through the CF Images admin interface:

  1. Process images in REST API - Enables image processing for REST requests (essential for headless)
  2. Process images in GraphQL responses - Handles GraphQL endpoints when WPGraphQL is active
  3. Process ACF image fields - Processes custom field images exposed via APIs
  4. Process content images - Handles inline images in post/page content

Frontend Framework Support

Works seamlessly with popular headless WordPress frameworks:

Next.js with Faust.js:

// Images automatically use Cloudflare URLs
export default function BlogPost({ post }) {
  return (
    <img 
      src={post.featuredImage.node.sourceUrl} // Cloudflare URL
      alt={post.featuredImage.node.altText}
    />
  );
}

Direct REST API consumption:

// Fetch post data - images are already processed
const response = await fetch('/wp-json/wp/v2/posts/123');
const post = await response.json();
// post.content.rendered contains Cloudflare image URLs

Testing & Validation

Comprehensive Test Suite (tests/test-faust-integration.php)

  • Integration initialization and auto-detection
  • REST API request processing
  • ACF field handling
  • Content image replacement
  • Configuration options testing

Validation Script (validate-headless-integration.php)

Provides automated health checks for:

  • Plugin activation status
  • Faust.js integration detection
  • REST API processing capability
  • Image URL transformation
  • Filter and hook registration

Documentation

Setup Guide (HEADLESS_SETUP.md)

Complete documentation covering:

  • Installation and configuration steps
  • WordPress backend setup requirements
  • Frontend framework integration patterns
  • Troubleshooting common issues
  • Performance optimization techniques

Usage Examples (EXAMPLE_USAGE.md)

Real-world implementation examples for:

  • Next.js and Faust.js integration
  • Nuxt.js headless setups
  • Custom React hooks for WordPress data
  • ACF field processing
  • Responsive image optimization

Backward Compatibility

This implementation maintains 100% backward compatibility. Existing WordPress installations continue to work exactly as before, while headless setups automatically gain Cloudflare image optimization capabilities.

Benefits

  • Zero Configuration: Works automatically when Faust.js plugin is detected
  • Framework Agnostic: Compatible with any frontend framework consuming WordPress APIs
  • Performance Optimized: Only processes requests when headless mode is active
  • Developer Friendly: Extensive hooks and filters for customization
  • Production Ready: Comprehensive error handling and fallback mechanisms

This enhancement positions the CF Images plugin as the premier solution for modern WordPress architectures, supporting both traditional and headless implementations with industry-leading image optimization.

Original prompt

This section details on the original issue you should resolve

<issue_title>Research: evaluate for headless support</issue_title>
<issue_description>Our headless wordpress sites use wordpress as the backend CMS. The frontends use Nuxt/vue and the faust plugin to do some url rewriting as necessary: https://github.com/wpengine/faustjs

Evaluate this plugin for properly supporting this setup for offloaded images to Cloudflare and what needs to be added to properly support this configuration. </issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #1

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 25, 2025 02:07
Co-authored-by: cpritchett <3866278+cpritchett@users.noreply.github.com>
…dPress support

Co-authored-by: cpritchett <3866278+cpritchett@users.noreply.github.com>
Copilot AI changed the title [WIP] Research: evaluate for headless support Add comprehensive headless WordPress and Faust.js support to CF Images plugin Sep 25, 2025
Copilot AI requested a review from cpritchett September 25, 2025 02:12
@cpritchett cpritchett marked this pull request as ready for review September 25, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Research: evaluate for headless support

2 participants