Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active September 21, 2023 13:41
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save westonruter/6c2ca0e5a4da233bf4bd88a1871dd950 to your computer and use it in GitHub Desktop.
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
<?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' );
@MontreePuttan
Copy link

ขอบคุณครับ

@sandeep816
Copy link

thank you!

@Eran-E
Copy link

Eran-E commented Nov 28, 2017

Hi, Thanks! I've also was looking for solution and that is a great one.
I've needed a flush button option because i didn't want to flush the cache every page load on the matched screens so i decided to fork it and make some additions.
I've added an option to choose between the current flush on page load or to flush with button on the admin bar, also on the button option: success notice on successful cache flush and more.
I'm new to Github so please correct me if i got it wrong but i can't create pull requests for gists, is it true? anyway, please check my fork: https://gist.github.com/Eran-E/365f10d3f967baaa29589fa1330cc129
i didn't add myself as an author.

@simonseddon
Copy link

Thanks for this buddy!

@hellotank
Copy link

Thanks for writing this up, saved me hours!

@prettyok
Copy link

Hi guys
Thank you for this plugin. Where do it put it? Do I add it to functions.php?
Thanks!

@fabioG2018
Copy link

Thanks guys! I put it in the functions.php and it works perfectly. Grazie mille

@kimlinhnguyen
Copy link

Hi Guys, Where do it put it? Do I add it to functions.php? I found many files named functions.php in the folder Plugins; Sorry but i'm a very newbie. Thanks for your answers

@westonruter
Copy link
Author

@kimlinhnguyen This is now part of WordPress core and so this plugin is obsolete.

@kimlinhnguyen
Copy link

@westonruter : I still have the issue : page template is not displaying in the menu attributs of the page; Could you pls advise me a solution ? Thanks

@westonruter
Copy link
Author

Sorry, I can't. Please reach out to the support forums: https://wordpress.org/support/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment