Skip to content

PHP deprecation: undefined $template->media property access during template save #834

@bph

Description

@bph

What

includes/create-theme/theme-templates.php:233 reads $template->media without checking whether the property exists on the WP_Block_Template object:

// Write the media assets if there are any
if ( $template->media ) {
    CBT_Theme_Media::add_media_to_local( $template->media );
}

The media property is only attached by prepare_template_for_export() on code paths where there is image media to localize. On every other path the property is simply absent — and this line reads it unconditionally.

When it triggers

  • PHP 8.2+ emits a Deprecated: Creation of dynamic property / Undefined property notice on every save where a template has no media to localize (i.e. most saves).
  • PHP 9 will turn this into a warning.
  • It does not currently break anything functionally — it just leaks notices into the PHP error log.

How it was discovered

Surfaced by a strict error handler in the new PHPUnit regression test added in #833. That test was narrowed to only catch the processOnlySavedTemplates regression so it does not fail on this unrelated pre-existing issue.

Suggested fix

One-line change at theme-templates.php:233:

- if ( $template->media ) {
+ if ( ! empty( $template->media ) ) {

! empty() returns false for both undefined and falsy values, so the existing behavior is preserved for the cases where the property is set.

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions