Last active
January 16, 2025 14:37
-
-
Save westonruter/6c2ca0e5a4da233bf4bd88a1871dd950 to your computer and use it in GitHub Desktop.
WP Trac #42573: Fix for theme template caching. https://core.trac.wordpress.org/ticket/42573
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin name: WP Trac #42573: Fix for theme template file caching. | |
* Description: Flush the theme file cache each time the admin screens are loaded which uses the file list. | |
* Plugin URI: https://core.trac.wordpress.org/ticket/42573 | |
* Author: Weston Ruter, XWP. | |
* Author URI: https://weston.ruter.net | |
*/ | |
function wp_42573_fix_template_caching( WP_Screen $current_screen ) { | |
// Only flush the file cache with each request to post list table, edit post screen, or theme editor. | |
if ( ! in_array( $current_screen->base, array( 'post', 'edit', 'theme-editor' ), true ) ) { | |
return; | |
} | |
$theme = wp_get_theme(); | |
if ( ! $theme ) { | |
return; | |
} | |
$cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() ); | |
$label = sanitize_key( 'files_' . $cache_hash . '-' . $theme->get( 'Version' ) ); | |
$transient_key = substr( $label, 0, 29 ) . md5( $label ); | |
delete_transient( $transient_key ); | |
} | |
add_action( 'current_screen', 'wp_42573_fix_template_caching' ); |
As @westonruter mentioned, it is now part of WordPress core. However, my new page template was still not showing up after adding it to the folder.
For me, I had two differently named page templates, but the Template Name between both was the same.
You can find this in the top level php tag. I updated the template name to something different and it immediately populated in my dropdown.
Source: https://vanseodesign.com/wordpress/wp-page-templates-dropdown/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry, I can't. Please reach out to the support forums: https://wordpress.org/support/