Blog

Page Template Bug in WordPress 4.9

When the latest version of WordPress (4.9) was released, it also contained a strange little bug that was introduced with some of the modifications to the internal “file editor” in WordPress. This bug caused page templates to either not display properly in the Page Edit screen or to not update until the default WordPress cache was cleared (60 minutes).  But there is a quick fix you can place into your theme’s functions.php file that will automatically clear the cache of the theme so you don’t have to wait for the cache to clear or raise your theme’s version number.

<?php
add_filter( 'init', 'wps_fix_page_template_cache_bug' );
function wps_fix_page_template_cache_bug(){
    $theme_root = dirname(__DIR__);
    $stylesheet = basename(__DIR__);
    $theme = wp_get_theme( $stylesheet, $theme_root );
    $theme_ver = $theme["Version"];
    $cache_hash = md5( $theme_root . '/' . $stylesheet);
    $label = sanitize_key( 'files_' . $cache_hash . '-' . $theme_ver );
    $transient_key = substr( $label, 0, 29 ) . md5( $label );
    delete_transient( $transient_key );
}

Thanks to the WordPress community for sharing variations of this fix.  Note: This bug will be fixed in an upcoming version of WordPress, so once that is fixed, you’ll be able to remove this code from your active theme’s functions.php file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.