render_block_core_comments( array $attributes, string $content, WP_Block $block ): string
- Since
- 6.1.0
- Source
wp-includes/blocks/comments.php:28
core/comments block on the server.Description
This render callback is mainly for rendering a dynamic, legacy version of this block (the old core/post-comments). It uses the comments_template() function to generate the output, in the same way as classic PHP themes.
As this callback will always run during SSR, first we need to check whether the block is in legacy mode. If not, the HTML generated in the editor is returned instead.
Compatibility
- WordPress
- since 6.1.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.
Parameters
$attributesarray- Block attributes.
$contentstring- Block default content.
$blockWP_Block- Block instance.
Return value
string- Returns the filtered post comments for the current post wrapped inside "p" tags.
Performance profile
How much work a call to render_block_core_comments() 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
- 8–80
- 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 86.
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()called directly - hookthird-party callbacks
apply_filters()one call below render_block_core_comments() - optionoption read or write
get_option()one call below render_block_core_comments()
Further down the call graph this can also reach 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_comments() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($value) | 8 | none |
isset($value) && !comments_open() | 20 | comments_open(), get_comments_number() |
isset($value) && comments_open() | 22–24 | comments_open(), ->render() |
isset($value) && !comments_open() | 28–30 | comments_open(), get_comments_number(), ->render() |
isset($value) && comments_open() | 67–74 | comments_open(), get_post(), setup_postdata(), ob_start(), add_filter(), comments_template(), remove_filter(), ob_get_clean(), class(), wp_enqueue_script(), enqueue_legacy_post_comments_block_styles(), sprintf() |
isset($value) && !comments_open() | 73–80 | comments_open(), get_comments_number(), get_post(), setup_postdata(), ob_start(), add_filter(), comments_template(), remove_filter(), ob_get_clean(), class(), wp_enqueue_script(), enqueue_legacy_post_comments_block_styles(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 86 | 8–80 | 7 | |
| 8.5 | 86 | 8–80 | 7 | |
| 8.4 | 86 | 8–80 | 7 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 89 | 8–83 | 7 | |
| 8.2 | 89 | 8–83 | 7 | |
| 8.1 | 89 | 8–83 | 7 | |
| 7.4 | 89 | 8–83 | 7 |
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 · 10
- comments_open()Determines whether the current post is open for comments.
- get_comments_number()Retrieves the amount of comments a post has.
- get_post()Retrieves post data given a post ID or post object.
- setup_postdata()Set up global post data.
- add_filter()Adds a callback function to a filter hook.
- comments_template()Loads the comment template specified in $file.
- remove_filter()Removes a callback function from a filter hook.
- 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.
- wp_enqueue_script()Enqueues a script.
- enqueue_legacy_post_comments_block_styles()Enqueues styles from the legacy `core/post-comments` block. These styles are required only by the block's fallback.
Source code
function render_block_core_comments( $attributes, $content, $block ) { global $post; if ( ! isset( $block->context['postId'] ) ) { return ''; } $post_id = $block->context['postId']; // Return early if there are no comments and comments are closed. if ( ! comments_open( $post_id ) && (int) get_comments_number( $post_id ) === 0 ) { return ''; } // If this isn't the legacy block, we need to render the static version of this block. $is_legacy = 'core/post-comments' === $block->name || ! empty( $attributes['legacy'] ); if ( ! $is_legacy ) { return $block->render( array( 'dynamic' => false ) ); } $post_before = $post; $post = get_post( $post_id ); setup_postdata( $post ); ob_start(); /* * There's a deprecation warning generated by WP Core. * Ideally this deprecation is removed from Core. * In the meantime, this removes it from the output. */ add_filter( 'deprecated_file_trigger_error', '__return_false' ); comments_template(); remove_filter( 'deprecated_file_trigger_error', '__return_false' ); $output = ob_get_clean(); $post = $post_before; $classnames = array(); // Adds the old class name for styles' backwards compatibility. if ( isset( $attributes['legacy'] ) ) { $classnames[] = 'wp-block-post-comments'; } if ( isset( $attributes['textAlign'] ) ) { $classnames[] = 'has-text-align-' . $attributes['textAlign']; } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); /* * Enqueues scripts and styles required only for the legacy version. That is * why they are not defined in `block.json`. */ wp_enqueue_script( 'comment-reply' ); enqueue_legacy_post_comments_block_styles( $block->name ); return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output );}Changelog
Introduced in 6.1.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 7.1.0 tag, from
src/wp-includes/blocks/comments.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.