WP Manifestindependent plugin directory
manifest / performance / cgit-wp-static-404

Castlegate IT WP Static 404 Page

Serves a cached 404 page to avoid excessive resource usage when serving dynamic 404 pages

by Castlegate IT · github.com/castlegateit/cgit-wp-static-404 · website

0stars
0forks

Install

No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:

wp plugin install https://github.com/castlegateit/cgit-wp-static-404/archive/refs/heads/main.zip

Readme

Castlegate IT WP Static 404

This plugin caches the WordPress 404 page and adjusts the default rewrite rules so that 404 requests can be served by a static HTML fil;e instead of a dynamically generated WordPress page.

This plugin handles page requests and file requests differently:

  • Files: Requests for common file extensions are handled by .htaccess rewrite rules. Without .htaccess support, this plugin will not be able to serve a static 404 page when a visitor requests a file that does not exist on your server. The default 404 handler is set in .htaccess and matching filenames are prevented from being routed via WordPress.
  • Pages: All other requests will be processed by WordPress. As soon as WordPress has detected that a 404 page should be served, the plugin includes the static 404 page and halts execution.

Caching the 404 page

The 404 page caching process is triggered by many different WordPress actions, often many are triggered per request and within quick succession.

To avoid regenerating the static 404 page file often, the plugin will create a cron task (if one does not already exist) to perform the cache operation. This cron will cache the 404 page after 30 minutes have passed, meaning that the cache should only be stale for a maximum of 30 minutes. The 30-minute delay avoids excessive requests to the WordPress 404 page by effectively limiting it to 48 requests per day.

If you're testing the plugin locally, the static 404 file will not generate unless your cron tasks are successfully executed. Typically, this will mean that your development URL needs to resolve on the server, so it needs adding to your hosts file.

Consider manually running outstanding cron jobs with wp cron event run cgit_cache_404. This will return "Invalid cron event" if a re-cache is not due.

Filters

Cache delay

A cron is scheduled after an action triggers a re-cache of the 404 page. This delay defines how long to wait before requesting a 404 page and re-caching. On websites with few updates and edits, this can be low to avoid potentially stale 404 pages. On websites with lots of updates, it should be higher to avoid triggering excessive requests to the 404 page for caching purposes. A default of 30 minutes is set to prevent more than 48 requests per day.

add_filter('cgit-static-404/cron-delay', function($delay) {
    return 3600;
});

404 file contents

After the 404 page contents have been fetched, filter the contents to modify it before saving the static file.

add_filter('cgit-static-404/response-contents', function($contents) {
    return 'My custom 404 content';
});

HTTP request timeout

Adjust the HTTP request timeout for requests made to the 404 page when attempting to cache it.

add_filter('cgit-static-404/http-timeout', function($timeout) {
    return 1;
});

Static 404 file name

Filter the static 404 page file name.

add_filter('cgit-static-404/404-file-name', function($filename) {
    return 'my-static-four-oh-four.html';
});

Static 404 file path

Filter the static 404 page file path.

add_filter('cgit-static-404/404-file-path', function($path) {
    return '/custom/file/path';
});

Static 404 file URL

Filter the static 404 page URL used by the plugin as the ErrorDocument 404 page

add_filter('cgit-static-404/404-url', function($url) {
    return '/my-custom/url/static-404.html';
});

Re-cache actions

Filter the actions that trigger a re-cache of the 404 page.

add_filter('cgit-static-404/recache-actions', function($actions) {
    $actions[] = 'my_custom_action';
    return $actions;
});

Request URL

Filter the URL used to trigger a 404 page to cache the page.

add_filter('cgit-static-404/request-url', function($url) {
    return get_home_url(null, 'my-custom-404-url');
});

File extensions

Filter the array of file extensions that should use the static 404 page instead of the WordPress one.

add_filter('cgit-static-404/404-file-extensions', function($extensions) {
    $extensions[] = 'md';
    $extensions[] = 'txt';

    return $extensions;
});

Read the full README on GitHub →

Releases

TagPublished
v1.0.0 Feb 27, 2025

These releases are tags only. The author does not attach a packaged zip, so there are no download counts to report.