close
Skip to content

Fatal Error in PHP 8.1 when a template part reference is missing #54352

@tomjn

Description

@tomjn

Description

Originally raised here: https://core.trac.wordpress.org/ticket/59318

If a block theme template references a template part via a template part block, but no part exists with the matching slug, a deprecation error occurs due to a null value being sent to preg replace.

In my particular case, the theme attribute was missing for unknown reasons on some templates loading the footer template part, and pages with those templates would trigger these fatal errors

Importantly, a check for this already exists, but only runs if the site is in debugging mode.

	// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
	// is set in `wp_debug_mode()`.
	$is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;

	if ( is_null( $content ) && $is_debug ) {

During debugging $content is indeed null but $is_debug is false, so the failure case is unhandled and null is passed to shortcode_unautop leading to the crash

Step-by-step reproduction instructions

Place this in a template:

<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

Notice that in my situation the reason this was not working was the missing theme attribute:

<!-- wp:template-part {"slug":"footer","theme":"my-theme"} /-->

It found the template part once theme had been added, but it should have failed gracefully and returned blank content instead of generating PHP fatal errors.

Screenshots, screen recording, code snippet

No response

Environment info

WP 6.3, PHP 8.1

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TestingNeeds further testing to be confirmed.[Block] Template PartAffects the Template Parts Block[Type] BugAn existing feature does not function as intended

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions