Skip to content

Ensure WP_Theme::$template is not null - #10837

Closed
IanDelMar wants to merge 3 commits into
WordPress:trunkfrom
IanDelMar:WP_Theme-template
Closed

Ensure WP_Theme::$template is not null#10837
IanDelMar wants to merge 3 commits into
WordPress:trunkfrom
IanDelMar:WP_Theme-template

Conversation

@IanDelMar

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/64582


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.

@github-actions

github-actions Bot commented Feb 2, 2026

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 marian1, westonruter.

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

@github-actions

github-actions Bot commented Feb 2, 2026

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

@westonruter

westonruter commented Sep 5, 2026

Copy link
Copy Markdown
Member

My first thought was that the WP_Theme::$template should be updated from ?string to string here with this change:

/**
* The directory name of the theme's files, inside the theme root.
*
* In the case of a child theme, this is the directory name of the parent theme.
* Otherwise, 'template' is the same as 'stylesheet'.
*
* @since 3.4.0
* @var ?string
*/
private $template;

But I see that it should indeed be left unset/null in the case of an error, where previously it short-circuits:

if ( is_array( $cache ) ) {
foreach ( array( 'block_template_folders', 'block_theme', 'errors', 'headers', 'template' ) as $key ) {
if ( isset( $cache[ $key ] ) ) {
$this->$key = $cache[ $key ];
}
}
if ( $this->errors ) {
return;
}

But I see you addressed this in the ticket description 👍🏻

Cover the `theme_child_invalid` branch of `WP_Theme::__construct()`, which
previously returned early without initializing `$this->template`.

The first test asserts the behaviors described on the ticket: `get_template()`
returns the stylesheet rather than `null`, and `get_template_directory()` and
`get_template_directory_uri()` resolve to the theme's own directory rather than
to the theme root. Before the fix the URI method also emitted a
`rawurlencode(): Passing null to parameter WordPress#1 ($string) of type string is
deprecated` notice.

The second test constructs the theme twice so the second instance is read back
from the themes cache, covering the `template` key added to the `cache_add()`
call. That path returns early on `$this->errors`, so without the cached value
the property would still be `null`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@westonruter

Copy link
Copy Markdown
Member

In cdb05c8 I added tests to cover the issues you covered in the "Resulting behaviour" section of the ticket.

Without the fixes you've added, there are test failures:

There were 2 failures:

1) Tests_Theme_wpTheme::test_child_theme_with_itself_as_parent_should_have_template_set
The template was not set to the stylesheet.
Failed asserting that null is identical to 'child-parent-itself'.

/var/www/tests/phpunit/tests/theme/wpTheme.php:202

2) Tests_Theme_wpTheme::test_child_theme_with_itself_as_parent_should_have_template_set_when_read_from_cache
The template was not restored from the cache.
Failed asserting that null is identical to 'child-parent-itself'.

/var/www/tests/phpunit/tests/theme/wpTheme.php:232

FAILURES!
Tests: 629, Assertions: 7358, Failures: 2, Skipped: 15.

And with your changes, they are fixed.

wporg-sync pushed a commit that referenced this pull request Sep 6, 2026
When a theme's `Template` header names the theme's own directory, `WP_Theme::__construct()` recorded the `theme_child_invalid` error and returned before assigning `$this->template`, leaving the property unset. As a result `WP_Theme::get_template()` returned `null` despite being documented as returning a string. That error scenario now assigns the stylesheet to `$this->template` and stores it in the theme cache, matching how the `theme_no_stylesheet` and `theme_no_parent` errors are already handled. The invalid-theme error itself is unchanged, and tests cover both the fresh and the cached construction paths.

Developed in #10837.
Follow-up to r41601.

Props marian1, westonruter.
See #40820.
Fixes #64582.


git-svn-id: https://develop.svn.wordpress.org/trunk@63507 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 63507
GitHub commit: 696f277

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Sep 6, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Sep 6, 2026
When a theme's `Template` header names the theme's own directory, `WP_Theme::__construct()` recorded the `theme_child_invalid` error and returned before assigning `$this->template`, leaving the property unset. As a result `WP_Theme::get_template()` returned `null` despite being documented as returning a string. That error scenario now assigns the stylesheet to `$this->template` and stores it in the theme cache, matching how the `theme_no_stylesheet` and `theme_no_parent` errors are already handled. The invalid-theme error itself is unchanged, and tests cover both the fresh and the cached construction paths.

Developed in WordPress/wordpress-develop#10837.
Follow-up to r41601.

Props marian1, westonruter.
See #40820.
Fixes #64582.

Built from https://develop.svn.wordpress.org/trunk@63507


git-svn-id: http://core.svn.wordpress.org/trunk@62683 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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.

2 participants