Make WordPress Core

Opened 36 hours ago

Last modified 36 hours ago

#64582 new defect (bug)

WP_Theme: Cyclic parent declaration can leave $template unset, causing inconsistencies

Reported by: marian1's profile marian1 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch
Focuses: Cc:

Description

If a theme's Template header points to the theme's own directory name, WP_Theme can end up with an unset/null $template property. The property’s documented type (?string) anticipates this, but related functions and magic behaviour do not.

Cause

In the theme_child_invalid branch, WP_Theme::__construct() sets the theme_child_invalid error and returns early without initialising $this->template.

Resulting behaviour

  • WP_Theme::get_template() returns null, despite being documented as returning string. This also affects callers such as WP_Customize_Manager::get_template(), which can therefore return string|null instead of the documented string.
  • isset($theme->template) returns true instead of false, because WP_Theme::__isset('template') only checks whether 'template' is a recognised property name, not whether it is actually set.
  • WP_Theme::get_template_directory() and WP_Theme::get_template_directory_uri() resolve to the theme root (with a trailing slash) rather than the theme's directory. WP_Theme::get_template_directory_uri() also triggers a deprecation warning because $this->template (null) is passed to rawurlencode(). Note that the global functions get_template_directory() and get_template_directory_uri() do return the theme's directory.

Proposed fix

This appears to be the only error where WordPress leaves the $template property unset. In other error scenarios (missing style.css, non-existent parent theme, etc.), a template value is still assigned, so these inconsistencies do not occur. I propose treating theme_child_invalid like theme_no_stylesheet or theme_no_parent:
In the theme_child_invalid early-return branch, set $this->template = $this->stylesheet; before returning. This keeps the invalid-theme error intact, but prevents WP_Theme::$template from causing the issues described above.

Change History (1)

This ticket was mentioned in PR #10837 on WordPress/wordpress-develop by @marian1.


36 hours ago
#1

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.