Skip to content

Enables caching on 404 pages to prevent performance issues related to bad request flooding.

License

Notifications You must be signed in to change notification settings

alleyinteractive/wp-404-caching

WP 404 Caching

Coding Standards Testing Suite

A WordPress plugin to provide full page caching for 404 pages, improving performance and reducing server load.

  • Contributors: alleyinteractive
  • Tags: alleyinteractive, wp-404-caching
  • Stable tag: 1.0.3

Requirements

  • SSL enabled on the website.
  • An external object cache setup (e.g., Redis, Memcached).
  • Requires at least: 6.3
  • Tested up to: 6.4
  • Requires PHP: 8.1
  • License: GPL v2 or later

Description

WP 404 Caching is a lightweight plugin that efficiently serves cached 404 pages to non-logged-in users. It reduces server load by storing the cached 404 page in an external object cache and returning it early in the request process.

The plugin uses a dual regular/stale caching strategy to minimize cache misses. It maintains a regular cache with a 1-hour expiration and a stale cache with a 1-day expiration. If the regular cache is empty, the stale cache is served.

Features

  • Full page caching for 404 pages
  • Utilizes external object cache for storing cached 404 pages
  • Dual regular/stale caching strategy to minimize cache misses
  • Automatically generates and caches 404 page via a "guaranteed 404 URI"
  • Triggers cache generation hourly via a cron job and immediately on cache misses
  • Sends X-WP-404-Cache header to indicate cache HIT/MISS status
  • Ensures compatibility with analytics by replacing the "guaranteed 404 URI" with the actual requested URI in the cached page

Installation

Via Composer (recommeded):

You can install the package via composer:

composer require alleyinteractive/wp-404-caching

Manual install:

  1. Upload the wp-404-caching directory to your /wp-content/plugins/ directory.
  2. Activate the plugin through the 'Plugins' menu in WordPress.

Usage

The plugin works out of the box with default settings. You can customize the cache expiration times by modifying the CACHE_TIME and STALE_CACHE_TIME constants in the Full_Page_Cache_404 class. Activate the plugin in WordPress and use it like so:

add_filter( 'wp_404_caching_cache_time', function( $cache_time ) {
    return 2 * HOUR_IN_SECONDS; // Set cache time to 2 hours.
} );

add_filter( 'wp_404_caching_stale_cache_time', function( $stale_cache_time ) {
    return 2 * DAY_IN_SECONDS; // Set stale cache time to 2 days.
} );

// Programmatically bypass the cache.
add_filter( 'wp_404_caching_enabled', '__return_false' );

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

This project is actively maintained by Alley Interactive. Like what you see? Come work with us.

License

The GNU General Public License (GPL) license. Please see License File for more information.


Developer Notes

Testing

Run composer test to run tests against PHPUnit and the PHP code in the plugin.

About

Enables caching on 404 pages to prevent performance issues related to bad request flooding.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Languages