WP_Theme::get_post_templates(): array[]
- Since
- 4.7.0, 5.8.0
- Source
wp-includes/class-wp-theme.php:1319
Compatibility
- WordPress
- since 5.8.0
- 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.
Return value
array[]- Array of page template arrays, keyed by post type and filename, with the value of the translated header name.
Performance profile
How much work a call to WP_Theme::get_post_templates() 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
- Light
- Scaling
- Scales with input
- Instructions
- 10–51
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.
Nothing here hands control to plugin code.
1 place 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 WP_Theme::get_post_templates()
Further down the call graph this can also reach option, query, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme::get_post_templates() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
->errors() | 10 | ->errors(), ->errors(), ->get_error_codes() |
!->errors() && is_array($post_templates) && !current_theme_supports() | 17–20 | ->errors(), ->cache_get(), current_theme_supports(), ->load_textdomain() |
->errors() && is_array($post_templates) && !current_theme_supports() | 23–26 | ->errors(), ->errors(), ->get_error_codes(), ->cache_get(), current_theme_supports(), ->load_textdomain() |
!->errors() && is_array($post_templates) && current_theme_supports() | 27–31 | ->errors(), ->cache_get(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain() |
!->errors() && !is_array($post_templates) && !current_theme_supports() | 30–34 | ->errors(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), ->load_textdomain() |
->errors() && is_array($post_templates) && current_theme_supports() | 33–37 | ->errors(), ->errors(), ->get_error_codes(), ->cache_get(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain() |
->errors() && !is_array($post_templates) && !current_theme_supports() | 36–40 | ->errors(), ->errors(), ->get_error_codes(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), ->load_textdomain() |
!->errors() && !is_array($post_templates) && current_theme_supports() | 40–45 | ->errors(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain() |
->errors() && !is_array($post_templates) && current_theme_supports() | 46–51 | ->errors(), ->errors(), ->get_error_codes(), ->cache_get(), ->get_files(), ->cache_add(), current_theme_supports(), get_block_templates(), get_post_types(), ->load_textdomain() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 131 | 10–51 | 23 | |
| 8.5 | 131 | 10–51 | 23 | |
| 8.4 | 131 | 10–51 | 23 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 134 | 10–51 | 23 | |
| 8.2 | 134 | 10–51 | 23 | |
| 8.1 | 134 | 10–51 | 23 | |
| 7.4 | 134 | 10–51 | 23 |
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 · 11
- _cleanup_header_comment()Strips close comment and close php tags from file headers used by WP.
- sanitize_key()Sanitizes a string key.
- current_theme_supports()Checks a theme's support for a given feature.
- get_block_templates()Retrieves a list of unified template objects based on a query.
- get_post_types()Gets a list of all registered post type objects.
- WP_Theme::errors()Returns errors property.
- WP_Theme::cache_get()Gets theme data from cache.
- WP_Theme::get_files()Returns files in the theme's directory.
- WP_Theme::cache_add()Adds theme data to cache.
- WP_Theme::load_textdomain()Loads the theme's textdomain.
- WP_Theme::translate_header()Translates a theme header.
Used by · 1
- WP_Theme::get_page_templates()Returns the theme's post templates for a given post type.
Source code
public function get_post_templates() { // If you screw up your active theme and we invalidate your parent, most things still work. Let it slide. if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) { return array(); } $post_templates = $this->cache_get( 'post_templates' ); if ( ! is_array( $post_templates ) ) { $post_templates = array(); $files = (array) $this->get_files( 'php', 1, true ); foreach ( $files as $file => $full_path ) { if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) { continue; } $types = array( 'page' ); if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) { $types = explode( ',', _cleanup_header_comment( $type[1] ) ); } foreach ( $types as $type ) { $type = sanitize_key( $type ); if ( ! isset( $post_templates[ $type ] ) ) { $post_templates[ $type ] = array(); } $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] ); } } $this->cache_add( 'post_templates', $post_templates ); } if ( current_theme_supports( 'block-templates' ) ) { $block_templates = get_block_templates( array(), 'wp_template' ); foreach ( get_post_types( array( 'public' => true ) ) as $type ) { foreach ( $block_templates as $block_template ) { if ( ! $block_template->is_custom ) { continue; } if ( isset( $block_template->post_types ) && ! in_array( $type, $block_template->post_types, true ) ) { continue; } $post_templates[ $type ][ $block_template->slug ] = $block_template->title; } } } if ( $this->load_textdomain() ) { foreach ( $post_templates as &$post_type ) { foreach ( $post_type as &$post_template ) { $post_template = $this->translate_header( 'Template Name', $post_template ); } } } return $post_templates; }Changelog
Introduced in 4.7.0. 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 6.7.7 tag, from
src/wp-includes/class-wp-theme.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.