wpseek.com
A WordPress-centric search engine for devs and theme authors



resolve_block_template › WordPress Function

Since5.8.0
Deprecatedn/a
resolve_block_template ( $template_type, $template_hierarchy )
Access:
  • private
Parameters: (2)
  • (string) $template_type The current template type.
    Required: Yes
  • (string[]) $template_hierarchy (optional) The current template hierarchy, ordered by priority.
    Required: Yes
Returns:
  • (WP_Block_Template|null) template A template object, or null if none could be found.
Defined at:
Codex:
Change Log:
  • Accepts

Return the correct 'wp_template' to render for the request template type.



Source

function resolve_block_template( $template_type, $template_hierarchy ) {
	if ( ! $template_type ) {
		return null;
	}

	if ( empty( $template_hierarchy ) ) {
		$template_hierarchy = array( $template_type );
	}

	$slugs = array_map(
		'_strip_template_file_suffix',
		$template_hierarchy
	);

	// Find all potential templates 'wp_template' post matching the hierarchy.
	$query     = array(
		'theme'    => wp_get_theme()->get_stylesheet(),
		'slug__in' => $slugs,
	);
	$templates = get_block_templates( $query );

	// Order these templates per slug priority.
	// Build map of template slugs to their priority in the current hierarchy.
	$slug_priorities = array_flip( $slugs );

	usort(
		$templates,
		function ( $template_a, $template_b ) use ( $slug_priorities ) {
			return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
		}
	);

	return count( $templates ) ? $templates[0] : null;
}