render_block_core_post_time_to_read( array $attributes, string $content, WP_Block $block ): string
- Since
- 6.9.0
- Source
wp-includes/blocks/post-time-to-read.php:100
core/post-time-to-read block on the server.Compatibility
- WordPress
- since 6.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attributesarray- Block attributes.
$contentstring- Block default content.
$blockWP_Block- Block instance.
Return value
string- Returns the rendered post author name block.
Performance profile
How much work a call to render_block_core_post_time_to_read() 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
- Heavy
- Scaling
- Constant
- Instructions
- 7–99
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_post().
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 131.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()one call below render_block_core_post_time_to_read() - hookthird-party callbacks
apply_filters()one call below render_block_core_post_time_to_read()
Further down the call graph this can also reach option, transient, cache and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_post_time_to_read() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($value) | 7 | none |
isset($value) && $display_mode !== "time" && $display_mode !== "words" | 45–47 | get_the_content(), wp_get_word_count_type(), block_core_post_time_to_read_word_count(), class(), sprintf() |
isset($value) && $display_mode !== "time" && $display_mode === "words" | 63–66 | get_the_content(), wp_get_word_count_type(), block_core_post_time_to_read_word_count(), _n(), number_format_i18n(), sprintf(), class(), sprintf() |
isset($value) && $display_mode === "time" && empty($attributes) && $display_mode !== "words" | 65–67 | get_the_content(), wp_get_word_count_type(), block_core_post_time_to_read_word_count(), round(), _n(), sprintf(), class(), sprintf() |
isset($value) && $display_mode === "time" && !empty($attributes) && $display_mode !== "words" | 76–80 | get_the_content(), wp_get_word_count_type(), block_core_post_time_to_read_word_count(), round(), round(), _x(), sprintf(), class(), sprintf() |
isset($value) && $display_mode === "time" && empty($attributes) && $display_mode === "words" | 83–86 | get_the_content(), wp_get_word_count_type(), block_core_post_time_to_read_word_count(), round(), _n(), sprintf(), _n(), number_format_i18n(), sprintf(), class(), sprintf() |
isset($value) && $display_mode === "time" && !empty($attributes) && $display_mode === "words" | 94–99 | get_the_content(), wp_get_word_count_type(), block_core_post_time_to_read_word_count(), round(), round(), _x(), sprintf(), _n(), number_format_i18n(), sprintf(), class(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 129 | 7–98 | 9 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 131 | 7–99 | 9 | |
| 8.4 | 131 | 7–99 | 9 | 16 fewer instructions than PHP 8.3 |
| 8.3 | 147 | 7–111 | 9 | |
| 8.2 | 147 | 7–111 | 9 | |
| 8.1 | 147 | 7–111 | 9 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 148 | 7–112 | 9 |
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 · 7
- get_the_content()Retrieves the post content.
- wp_get_word_count_type()Retrieves the word count type based on the locale.
- block_core_post_time_to_read_word_count()Counts words or characters in a provided text string.
- _x()Retrieves translated string with gettext context.
- _n()Translates and retrieves the singular or plural form based on the supplied number.
- number_format_i18n()Converts float number to format based on the locale.
- get_block_wrapper_attributes()Generates a string of attributes by applying to the current block being rendered all of the features that the block supports.
Source code
function render_block_core_post_time_to_read( $attributes, $content, $block ) { if ( ! isset( $block->context['postId'] ) ) { return ''; } $content = get_the_content(); $average_reading_rate = $attributes['averageReadingSpeed'] ?? 189; $display_mode = $attributes['displayMode'] ?? 'time'; $word_count_type = wp_get_word_count_type(); $total_words = block_core_post_time_to_read_word_count( $content, $word_count_type ); $parts = array(); // Add "time to read" part, if enabled. if ( 'time' === $display_mode ) { if ( ! empty( $attributes['displayAsRange'] ) ) { // Calculate faster reading rate with 20% speed = lower minutes, // and slower reading rate with 20% speed = higher minutes. $min_minutes = max( 1, (int) round( $total_words / $average_reading_rate * 0.8 ) ); $max_minutes = max( 1, (int) round( $total_words / $average_reading_rate * 1.2 ) ); if ( $min_minutes === $max_minutes ) { $max_minutes = $min_minutes + 1; } /* translators: 1: minimum minutes, 2: maximum minutes to read the post. */ $time_string = sprintf( /* translators: 1: minimum minutes, 2: maximum minutes to read the post. */ _x( '%1$s–%2$s minutes', 'Range of minutes to read' ), $min_minutes, $max_minutes ); } else { $minutes_to_read = max( 1, (int) round( $total_words / $average_reading_rate ) ); $time_string = sprintf( /* translators: %s: the number of minutes to read the post. */ _n( '%s minute', '%s minutes', $minutes_to_read ), $minutes_to_read ); } $parts[] = $time_string; } // Add "word count" part, if enabled. if ( 'words' === $display_mode ) { $word_count_string = 'words' === $word_count_type ? sprintf( /* translators: %s: the number of words in the post. */ _n( '%s word', '%s words', $total_words ), number_format_i18n( $total_words ) ) : sprintf( /* translators: %s: the number of characters in the post. */ _n( '%s character', '%s characters', $total_words ), number_format_i18n( $total_words ) ); $parts[] = $word_count_string; } $display_string = implode( '<br>', $parts ); $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $display_string );}Changelog
Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/blocks/post-time-to-read.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.