Skip to content

Themes: Optimize WP_Theme::get_post_templates() for efficiency#11545

Open
MarcinDudekDev wants to merge 1 commit intoWordPress:trunkfrom
MarcinDudekDev:trac/42513
Open

Themes: Optimize WP_Theme::get_post_templates() for efficiency#11545
MarcinDudekDev wants to merge 1 commit intoWordPress:trunkfrom
MarcinDudekDev:trac/42513

Conversation

@MarcinDudekDev
Copy link
Copy Markdown

Summary

  • 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() query inside the cache block so the DB query only fires on a cache miss
  • Add unit tests covering caching behavior, get_file_data() integration, and cache invalidation on theme switch

Details

WP_Theme::get_post_templates() previously called file_get_contents() twice per PHP file — once for Template Name and once for Template Post Type — reading the entire file each time. This replaces both calls with a single get_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 a WP_Query with posts_per_page=-1 on 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

  • Existing Tests_Admin_IncludesTheme tests pass (8/8, 55 assertions)
  • PHPCS clean on both modified files
  • Single-line template headers (<?php // Template Name: ... ?>) parsed correctly
  • Multi-line docblock headers parsed correctly
  • Template Post Type with multiple types parsed correctly
  • Child theme template inheritance works correctly
  • Verify performance improvement on a theme with many PHP files

AI 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

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>
@github-actions
Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props myththrazz.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Hi @MarcinDudekDev! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

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,
The WordPress Project

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant