Improve performance of _register_theme_block_patterns() function
_register_theme_block_patterns() function previously introduced a substantial resource overhead issue, especially noticeable when themes, such as Twenty Twenty Four, registered a large number of block patterns. This overhead was mainly due to the extensive file operations required by the function, including file system checks and file reads.
To address these performance issues, we have introduced caching using a transient in a new function called
_wp_get_block_patterns(). When theme development mode is disabled and a theme exists, the block patterns are stored in a transient cache. Subsequent requests will use this cached data, eliminating the need for file lookups and reading files into memory, resulting in significant performance improvements.
It’s important to note that caches are also tied to the theme’s version number. Therefore, if you want to manually invalidate the cache, you can do so by changing the theme’s version number. This provides an additional method for cache management, especially useful during theme updates or changes.
Developers can bypass the pattern cache by enabling development mode for a theme, allowing for pattern additions and removals without the cache interfering. This is beneficial for accurate testing and development.
Cache invalidation occurs when themes are switched, the
WP_Theme::delete_caches() method is called, or when the theme’s version number is updated. These mechanisms ensure that block patterns are not stored in the cache incorrectly.
Remove unnecessary checks if a theme file exists in the theme functions
Previously, several functions and methods in the Themes API were designed to check for the existence of files within a child theme before falling back to the parent theme.
However, these checks didn’t take into account whether the current theme was a child theme or not, leading to redundant file existence checks for non-child themes. This could result in unnecessary file system access and potentially slow down WordPress websites, especially in cases where there were numerous checks.
To address this issue and improve the overall performance of the Themes API, we’ve introduced an optimization that checks whether the current theme’s stylesheet directory matches the template directory before proceeding with file existence checks. This optimization significantly reduces the number of unnecessary file system accesses, as file existence checks can be resource-intensive in PHP.
As part of this enhancement, we’ve updated the following functions and methods in WordPress:
With these optimizations in core, you can expect a noticeable improvement in the performance of your WordPress theme, especially if you have a non-child theme. The reduction in unnecessary file system access can lead to faster page load times and a smoother user experience.
Improve performance of get_block_theme_folders()
get_block_theme_folders() function suffered from suboptimal performance due to repeated file lookups using the
file_exists() function. This resulted in unnecessary I/O operations and slowed down the process of locating block template folders within themes.
To address these performance issues and improve the efficiency of the
get_block_theme_folders() function, the following changes have been implemented:
- A new method,
WP_Theme::get_block_template_folders(), has been added which incorporates basic caching, storing the result in the theme’s cache, similar to how block themes are cached in the block_theme property (as outlined in ). The introduction of caching significantly reduces redundant file system lookups and optimizes the overall performance of the function.
- Improved Error Handling: This update enhances error handling by first verifying the existence of a theme before attempting to look up the file. This proactive error handling approach helps avoid unnecessary file checks and enhances the overall reliability of the function.
The performance enhancement in the
WP_Theme::get_block_template_folders() method will lead to quicker and more efficient lookups of block template folders within themes. WordPress developers and users can anticipate improved performance, reduced I/O overhead, and a smoother experience when working with block themes.
Bundled Theme: Implement the_header_image_tag() function for enhanced compatibility for older core themes.
the_header_image_tag() function was introduced in WordPress 4.4 as part of . It is used in all themes created since WordPress 4.4 that supported header images. The function
get_header_image_tag() continues to get updated with new image features, like lazy loading, async decoding and fetch priority. To ensure our core themes maintain compatibility and benefit from these enhancements, a backward compatibility shim has been applied, integrating the
the_header_image_tag() function into the following core themes:
- Twenty Ten
- Twenty Eleven
- Twenty Twelve
- Twenty Fourteen
- Twenty Sixteen
This change ensures future compatibility and modern image features are applied for header images to these older themes.