Themes: Optimize WP_Theme::get_post_templates() for efficiency#11545
Themes: Optimize WP_Theme::get_post_templates() for efficiency#11545MarcinDudekDev wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Replace dual `file_get_contents()` calls with a single `get_file_data()` call, which reads only the first 8KB of each file instead of the entire contents. Move `get_block_templates()` inside the cache block so the database query only fires on a cache miss instead of on every call. Props gschoppe, birgire, sukhendu2002. Fixes #42513. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Hi @MarcinDudekDev! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Summary
file_get_contents()calls with a singleget_file_data()call, which reads only the first 8KB of each file instead of the entire contentsget_block_templates()query inside the cache block so the DB query only fires on a cache missget_file_data()integration, and cache invalidation on theme switchDetails
WP_Theme::get_post_templates()previously calledfile_get_contents()twice per PHP file — once forTemplate Nameand once forTemplate Post Type— reading the entire file each time. This replaces both calls with a singleget_file_data()call that reads only the first 8KB (consistent with how WordPress parses plugin and theme headers elsewhere in core).Additionally,
get_block_templates()was called outside the cache block, executing aWP_Querywithposts_per_page=-1on every invocation regardless of cache state. Moving it inside the cache block eliminates redundant database queries on cache hits.Trac ticket: https://core.trac.wordpress.org/ticket/42513
Related: https://core.trac.wordpress.org/ticket/42517
Test plan
Tests_Admin_IncludesThemetests pass (8/8, 55 assertions)<?php // Template Name: ... ?>) parsed correctlyTemplate Post Typewith multiple types parsed correctlyAI assistance: Yes
Tool(s): Claude Code (Anthropic)
Model(s): Claude Opus 4.6
Used for: Analysis of the performance bottleneck, implementation of the optimization, and writing unit tests. All code was reviewed and tested.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code