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: |
|
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()returnsnull, despite being documented as returningstring. This also affects callers such asWP_Customize_Manager::get_template(), which can therefore returnstring|nullinstead of the documentedstring.isset($theme->template)returnstrueinstead offalse, becauseWP_Theme::__isset('template')only checks whether'template'is a recognised property name, not whether it is actually set.WP_Theme::get_template_directory()andWP_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 torawurlencode(). Note that the global functionsget_template_directory()andget_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
Trac ticket: https://core.trac.wordpress.org/ticket/64582