Skip to content

Commit 7f74328

Browse files
authored
Disable caching for wp_die() error pages (#1015) (#1036)
* Disable caching for wp_die() error pages Fixes #1015 Hook wp_die_handler to define DONOTCACHEPAGE when wp_die() is invoked, preventing error/interstitial pages (e.g. "Error establishing a Redis connection") from being cached and served to subsequent visitors. A wpsc_disable_cache_on_wp_die filter preserves the legacy behavior for sites that intentionally render cacheable content via wp_die(). * Linting warning fix
1 parent bf44af1 commit 7f74328

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

wp-cache.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ function wp_super_cache_init_action() {
157157
}
158158
add_action( 'init', 'wp_super_cache_init_action' );
159159

160+
/**
161+
* Disable caching for pages rendered via wp_die().
162+
*
163+
* The function wp_die() is used to render error and interstitial pages (e.g.
164+
* "Error establishing a database connection"); caching them causes the error to
165+
* persist for subsequent visitors even after the underlying issue is resolved.
166+
*
167+
* @param callable $handler The registered wp_die handler, returned unchanged.
168+
* @return callable
169+
*/
170+
function wpsc_wp_die_disable_cache( $handler ) {
171+
/**
172+
* Filters whether to disable caching when wp_die() is invoked.
173+
*
174+
* @param bool $disable Whether to set DONOTCACHEPAGE. Default true.
175+
*/
176+
if ( apply_filters( 'wpsc_disable_cache_on_wp_die', true ) && ! defined( 'DONOTCACHEPAGE' ) ) {
177+
define( 'DONOTCACHEPAGE', true );
178+
}
179+
return $handler;
180+
}
181+
add_filter( 'wp_die_handler', 'wpsc_wp_die_disable_cache' );
182+
160183
function wp_cache_set_home() {
161184
global $wp_cache_is_home;
162185
$wp_cache_is_home = ( is_front_page() || is_home() );

0 commit comments

Comments
 (0)