0

解决 fun88体育 4.9 页面模板功能无法正常使用

fun88体育 4.9 有一个重要更新是:在WP后台编辑主题和插件文件时,支持按层级显示所有文件。为了性能考虑,使用了 transient 缓存机制。但是却没有提供任何手动清除缓存的功能,导致有些用户升级到 fun88体育 4.9 以后,发现“页面属性 – 模板”这个功能不能正常使用了,不显示主题自带的模板文件!


要解决这个问题,我们可以添加下面的代码到当前主题的 functions.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.fun88体育.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' );