/ 2 min read

Caching Contact Form 7 Schema Requests

If you’ve noticed performance issues on WordPress pages that contain a Contact Form 7 form, especially during traffic spikes, you might be experiencing an overlooked problem: uncached API requests.

Most (free) caching plugins focus on full-page caching and typically miss the REST API endpoints.

The Problem

Every time a visitor loads a page containing a Contact Form 7 form, WordPress makes a GET request to an endpoint like this: /wp-json/contact-form-7/v1/contact-forms/{form_id}/feedback/schema This request fetches the form schema needed for client-side validation. Since these are API requests rather than full page loads, most caching plugins ignore them. On high-traffic sites or during traffic spikes (like after sending a newsletter), these uncached requests can quickly overwhelm your server. Oddly, the Contact Form 7 developers chose to load this schema via AJAX rather than including it directly with the form HTML, creating this potential bottleneck.

The Solution: WP REST Cache

Fortunately, there’s a straightforward solution: the WP REST Cache plugin. It’s free and designed specifically for WordPress REST API endpoints and can be configured to cache Contact Form 7’s schema requests while leaving form submissions (POST requests) untouched. By default, WP REST Cache doesn’t cache Contact Form 7 endpoints, but with a simple code snippet, we can tell it exactly which endpoints to handle.

The Code

<?php

add_filter( 'wp_rest_cache/allowed_endpoints', function ( $allowed_endpoints ) {
	if ( ! isset( $allowed_endpoints['contact-form-7/v1'] ) ) {
		$allowed_endpoints['contact-form-7/v1'] = [];
	}

	if ( ! in_array( 'contact-forms', $allowed_endpoints['contact-form-7/v1'], true ) ) {
		$allowed_endpoints['contact-form-7/v1'][] = 'contact-forms';
	}

	return $allowed_endpoints;
}, 10, 1 );

This snippet hooks into the wp_rest_cache/allowed_endpoints filter, adding the Contact Form 7 namespace and endpoints to the list of cacheable requests. You can add it to your theme’s functions.php file or a site-specific plugin.

After adding the code, load a page containing a form (Logged out) and then go to the plugins settings page and check the ‘Endpoint API Caches’ tab. You should now see the schema request for the form embedded on the page in the list of cached requests. You can change the general settings to set how long the cache lasts.

Performance Benefits

After implementing this solution, you should notice:

  • Reduced server load, particularly during traffic spikes
  • Faster page load times for pages containing forms
  • More consistent performance when sending newsletters or sharing pages containing forms