_build_block_template_object_from_post_object( WP_Post $post, array $terms = array(), array $meta = array() ): WP_Block_Template|WP_Error
- Since
- 6.5.3
- Source
wp-includes/block-template-utils.php:845
Description
This is a helper function that creates a block template object from a given post object.
It is self-sufficient in that it only uses information passed as arguments; it does not query the database for additional information.
Compatibility
- WordPress
- since 6.5.3
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$postWP_Post- Template post.
$termsarrayoptional- Additional terms to inform the template object.Default:
array() $metaarrayoptional- Additional meta fields to inform the template object.Default:
array()
Return value
WP_Block_Template|WP_Error- Template or error object.
Performance profile
How much work a call to _build_block_template_object_from_post_object() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 13–103
- Plugin surface
- None
- Called by
- 2
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below _build_block_template_object_from_post_object() - optionoption read or write
get_option()one call below _build_block_template_object_from_post_object()
Further down the call graph this can also reach cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _build_block_template_object_from_post_object() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($terms) | 13 | __() |
!empty($terms) | 85–103 | get_default_block_template_types(), _get_block_template_file(), get_stylesheet() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 112 | 13–103 | 10 | |
| 8.5 | 112 | 13–103 | 10 | |
| 8.4 | 112 | 13–103 | 10 | |
| 8.3 | 112 | 13–103 | 10 | |
| 8.2 | 112 | 13–103 | 10 | |
| 8.1 | 112 | 13–103 | 10 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 113 | 13–104 | 10 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 6
- __()Retrieves the translation of $text.
- get_default_block_template_types()Returns a filtered list of default template types, containing their localized titles and descriptions.
- _get_block_template_file()Retrieves the template file from the theme for a given slug.
- get_stylesheet()Retrieves name of the current stylesheet.
- WP_Error::__construct()Initializes the error.
- WP_Block_Template::__construct()
Used by · 2
- _build_block_template_result_from_post()Builds a unified template object based a post Object.
- inject_ignored_hooked_blocks_metadata_attributes()Inject ignoredHookedBlocks metadata attributes into a template or template part.
Source code
function _build_block_template_object_from_post_object( $post, $terms = array(), $meta = array() ) { if ( empty( $terms['wp_theme'] ) ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } $theme = $terms['wp_theme']; $default_template_types = get_default_block_template_types(); $template_file = _get_block_template_file( $post->post_type, $post->post_name ); $has_theme_file = get_stylesheet() === $theme && null !== $template_file; $template = new WP_Block_Template(); $template->wp_id = $post->ID; $template->id = $theme . '//' . $post->post_name; $template->theme = $theme; $template->content = $post->post_content; $template->slug = $post->post_name; $template->source = 'custom'; $template->origin = ! empty( $meta['origin'] ) ? $meta['origin'] : null; $template->type = $post->post_type; $template->description = $post->post_excerpt; $template->title = $post->post_title; $template->status = $post->post_status; $template->has_theme_file = $has_theme_file; $template->is_custom = empty( $meta['is_wp_suggestion'] ); $template->author = $post->post_author; $template->modified = $post->post_modified; $template->date = $post->post_date; if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { $template->post_types = $template_file['postTypes']; } if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { $template->is_custom = false; } if ( 'wp_template_part' === $post->post_type && isset( $terms['wp_template_part_area'] ) ) { $template->area = $terms['wp_template_part_area']; } return $template;}Changelog
Introduced in 6.5.3. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/block-template-utils.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.