Solve the problem that WordPress 4.9 page template function cannot be used normally

Today, I encountered a problem about the page template can not be used. I found a lot of relevant information, but it can not be solved. Finally, even if the problem in the WordPress theme is fixed, the link address of the relevant page still needs to be changed to solve it. The relevant records and extracts are as follows:

An important update of WordPress 4.9 is that when editing theme and plugin files in the WP background, all files can be displayed hierarchically. For the sake of performance, the “transient” caching mechanism is used. However, it does not provide any function to clear the cache manually. As a result, some users found that the function of “page properties template” could not be used normally after upgrading to WordPress 4.9, and the template file of the theme was not displayed!

To solve this problem, we can add the following code to the functions.php That’s it:

/**
 * 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' );

If you are not a developer, it is recommended that you use the following plugins and install them directly.

link: https://pan.baidu.com/s/1wodkdaxibmb3jrwkupzksw Password: RTOD

 

The above excerpts are as follows: https://www.wpdaxue.com/fix-for-theme-template-file-caching.html

Note:

This problem only appears in version 4.9, and has been fixed since 4.9.1. However, in my client’s website, although the version is upgraded from 4.9 to 4.9.4, and the plugin is also installed, the page template cannot be used in the original links of the existing pages. Processing method is: create a new page for a page address (namely link alias). In this way, the problem is solved. Only if the record, if you also found this problem, you can try the above method.

Related Article
How to get the number of articles and comments of the current logined user in WordPress

When developing WordPress Theme user center, I want to get the number of articles and comments of current logged in Read more

WordPress tips: get statistics of the number of pictures in the article

As far as I know, WordPress has no function to count the number of pictures in the article, so to Read more

WordPress details optimization to automatically add ALT and title information to the article image

Friends who know a little bit about SEO optimization should understand that image ALT and image title optimization are very Read more

how to automatically add tag links to the article

Today, I'd like to share a WordPress SEO optimization tip, which can realize the automatic addition of keyword tags in Read more

Leave a Reply

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

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image