Instantly share code, notes, and snippets.

Embed
What would you like to do?
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' );
@Exonfang

This comment has been minimized.

Exonfang commented Nov 22, 2017

Thank you for writing this plugin, I'll never understand why Wordpress thought it would be a good idea to cache the template files without giving devs an option to disable the caching natively.

@jasonpau

This comment has been minimized.

jasonpau commented Nov 23, 2017

Thanks so much! I was about to dig into all kinds of major rabbit trails to solve this "bug" when I came across your fix on stackoverflow.

@jhkoning

This comment has been minimized.

jhkoning commented Nov 23, 2017

Brilliant - thank you!

@kbell1111

This comment has been minimized.

kbell1111 commented Nov 24, 2017

omg thank you!!! you just saved us so much struggle on a project because of this darn cache issue. do you have a blog or somewhere we can donate some coffee money? this is truly so appreciated!

@emfluenceindia

This comment has been minimized.

emfluenceindia commented Nov 27, 2017

Wow! It fixes the issue. Thank you so much!

@bambattajb

This comment has been minimized.

bambattajb commented Nov 27, 2017

I was just doing some work on a site and created + named a new template file, assigned a page to it, then decided to rename the template file. WordPress was then trying to find the old template file which no longer exists.

This Gist fixes the problem.

@anormore

This comment has been minimized.

anormore commented Nov 27, 2017

Thank you, works great. Just threw this on to functions.php and noted it to be removed at some point.

@emlogtech

This comment has been minimized.

emlogtech commented Nov 28, 2017

Hi Everyone,
I am a beginner with WordPress. I just downloaded and activated this plugin. How does it help me resolve this issue "Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over FTP"? Kindly assist me.
unable to update issue

@maddisondesigns

This comment has been minimized.

maddisondesigns commented Nov 28, 2017

Thanks for this temp fix. Just wasted way too much time on this issue!

@MontreePuttan

This comment has been minimized.

MontreePuttan commented Nov 28, 2017

ขอบคุณครับ

@sandeepvaishnav

This comment has been minimized.

sandeepvaishnav commented Nov 28, 2017

thank you!

@auermi

This comment has been minimized.

auermi commented Nov 28, 2017

This is great, thanks so much!

@Eran-E

This comment has been minimized.

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

This comment has been minimized.

simonseddon commented Nov 29, 2017

Thanks for this buddy!

@hellotank

This comment has been minimized.

hellotank commented Nov 29, 2017

Thanks for writing this up, saved me hours!

@prettyok

This comment has been minimized.

prettyok commented Jan 28, 2018

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

@fabioG2018

This comment has been minimized.

fabioG2018 commented Mar 26, 2018

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

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