WP_REST_Templates_Controller::get_template_fallback( WP_REST_Request $request ): WP_REST_Response|WP_Error

In this article

Returns the fallback template for the given slug.

Parameters

$requestWP_REST_Requestrequired
The request instance.

Return

WP_REST_Response|WP_Error

Source

public function get_template_fallback( $request ) {
	$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );

	do {
		$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
		array_shift( $hierarchy );
	} while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) );

	// To maintain original behavior, return an empty object rather than a 404 error when no template is found.
	$response = $fallback_template ? $this->prepare_item_for_response( $fallback_template, $request ) : new stdClass();

	return rest_ensure_response( $response );
}

Changelog

VersionDescription
6.3.0Ignore empty templates.
6.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.