Fix the template caching issue in WordPress 4.9 with a custom function to ensure visitors always see the latest version of your theme. Learn how to enhance caching for a smoother user experience.
Quick heads up before you copy anything: this one is old. It dates to WordPress 4.9, which shipped back in 2017. The snippet still runs, but the problem it chases barely shows up on a modern install. Read the note at the bottom before you paste it anywhere.
What this was fixing
The stale file-list problem
WordPress caches the list of files in your active theme in a transient, so it does not have to scan the theme folder on every admin page load. That is fine, until you drop a new template file into the theme over FTP and it does not show up in the editor or the page-template dropdown. The cache is still handing back the old list.
One thing worth correcting from the way this used to be explained: that cache is admin-side only. It is a list of theme files, not your rendered pages. Visitors were never seeing a stale theme because of it. The friction was yours, inside wp-admin, waiting for a new template to appear.
The snippet
Drop this into your active theme’s functions.php. It rebuilds the same transient key WordPress uses and deletes it while you are on a post, edit, or theme-editor screen, which forces the file list to get rescanned.
<?php
/**
* Refines the template caching mechanism to ensure the latest version of the theme is served.
*
* This function addresses an issue with WordPress template caching where outdated theme files
* may be served to visitors. It generates a unique cache signature based on the active theme's
* directory and stylesheet, and then removes the cached version, ensuring that updates are applied.
*
* @param WP_Screen $current_screen The current admin screen object.
*/
function refine_template_caching( WP_Screen $current_screen ) {
// Only apply this function on post, edit, or theme-editor screens.
if ( ! in_array( $current_screen->base, array( 'post', 'edit', 'theme-editor' ), true ) )
return;
// Get the current active theme.
$current_theme = wp_get_theme();
// If no active theme is found, exit the function.
if ( ! $current_theme )
return;
// Create a unique cache signature based on the theme's root and stylesheet.
$cache_signature = md5( $current_theme->get_theme_root() . '/' . $current_theme->get_stylesheet() );
// Create a sanitized transient key using the cache signature and the theme's version.
$key_label = sanitize_key( 'files_' . $cache_signature . '-' . $current_theme->get( 'Version' ) );
$transient_key = substr( $key_label, 0, 29 ) . md5( $key_label );
// Delete the transient key, ensuring the cache is refreshed.
delete_transient( $transient_key );
}
// Hook the function into the 'current_screen' action to run it when the screen is loaded.
add_action( 'current_screen', 'refine_template_caching' );What it does, in plain terms
- Screen check: it only runs on the post, edit, and theme-editor screens, and bails everywhere else.
- Grabs the active theme: via
wp_get_theme(), and exits if there somehow is not one. - Rebuilds the key: it hashes the theme root, stylesheet, and version into the same transient key WordPress stores the file list under.
- Clears it:
delete_transient()drops the cached list so the folder gets scanned fresh.
Should you still use this?
Probably not, and here is the honest version. That theme-file transient still exists in current WordPress, but it only lives for 30 minutes and it clears itself when you switch or update a theme. So the worst case today is a short wait, not a permanent stale list. Deleting the transient on every single admin screen load, the way this snippet does, is a heavy hammer for a small nail. If you are actively editing template files and a new one refuses to appear, it is simpler to just toggle the theme off and on. Keep this in your back pocket for that rare stubborn case, and pull it back out once the file shows up.


