[wp-trac] [WordPress Trac] #64582: WP_Theme: Cyclic parent declaration can leave $template unset, causing inconsistencies

WordPress Trac noreply at wordpress.org
Mon Feb 2 02:21:58 UTC 2026


#64582: WP_Theme: Cyclic parent declaration can leave $template unset, causing
inconsistencies
--------------------------+-----------------------------
 Reporter:  marian1       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Themes        |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/64582>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list