render_block_core_categories( array $attributes, string $content, WP_Block $block ): string
- Since
- 5.0.0, 6.7.0
- Source
wp-includes/blocks/categories.php:20
core/categories block on server.Compatibility
- WordPress
- since 6.7.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- The block attributes.
$contentstring- Block default content.
$blockWP_Block- Block instance.
Return value
string- Returns the categories list/dropdown markup.
Performance profile
How much work a call to render_block_core_categories() 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
- Scales with input
- Instructions
- 58–119
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_terms().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 150.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()one call below render_block_core_categories() - querycontent query
get_terms()one call below render_block_core_categories() - optionoption read or write
get_option()one call below render_block_core_categories()
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_categories() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($attributes) && empty($value) | 58–62 | get_taxonomy(), wp_list_categories(), class(), sprintf() |
empty($attributes) && !empty($value) && !->next_tag() | 70–74 | get_taxonomy(), wp_list_categories(), echo(), ->next_tag(), ->get_updated_html(), class(), sprintf() |
is_admin() | 97–102 | get_taxonomy(), __(), sprintf(), get_query_var(), esc_attr(), wp_dropdown_categories(), is_admin(), class(), sprintf() |
!empty($attributes) && is_admin() | 103–108 | get_taxonomy(), __(), sprintf(), get_query_var(), wp_kses_post(), esc_attr(), wp_dropdown_categories(), is_admin(), class(), sprintf() |
!is_admin() | 108–113 | get_taxonomy(), __(), sprintf(), get_query_var(), esc_attr(), wp_dropdown_categories(), is_admin(), build_dropdown_script_block_core_categories(), preg_replace(), class(), sprintf() |
!empty($attributes) && !is_admin() | 114–119 | get_taxonomy(), __(), sprintf(), get_query_var(), wp_kses_post(), esc_attr(), wp_dropdown_categories(), is_admin(), build_dropdown_script_block_core_categories(), preg_replace(), class(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 149 | 58–118 | 8 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 150 | 58–119 | 8 | |
| 8.4 | 150 | 58–119 | 8 | |
| 8.3 | 150 | 58–119 | 8 | |
| 8.2 | 150 | 58–119 | 8 | |
| 8.1 | 150 | 58–119 | 8 | |
| 7.4 | 150 | 58–119 | 8 |
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
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- __()Retrieves the translation of $text.
- get_query_var()Retrieves the value of a query variable in the WP_Query class.
- wp_kses_post()Sanitizes content for allowed HTML tags for post content.
- esc_attr()Escaping for HTML attributes.
- wp_dropdown_categories()Displays or retrieves the HTML dropdown list of categories.
- is_admin()Determines whether the current request is for an administrative interface page.
- build_dropdown_script_block_core_categories()Generates the inline script for a categories dropdown field.
- wp_list_categories()Displays or retrieves the HTML list of categories.
- 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_HTML_Tag_Processor::__construct()Constructor.
Source code
function render_block_core_categories( $attributes, $content, $block ) { static $block_id = 0; ++$block_id; $taxonomy = get_taxonomy( $attributes['taxonomy'] ); $args = array( 'echo' => false, 'hierarchical' => ! empty( $attributes['showHierarchy'] ), 'orderby' => 'name', 'show_count' => ! empty( $attributes['showPostCounts'] ), 'taxonomy' => $attributes['taxonomy'], 'title_li' => '', 'hide_empty' => empty( $attributes['showEmpty'] ), ); if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { $args['parent'] = 0; } if ( ! empty( $attributes['displayAsDropdown'] ) ) { $id = 'wp-block-categories-' . $block_id; $args['id'] = $id; $args['name'] = $taxonomy->query_var; $args['value_field'] = 'slug'; $args['show_option_none'] = sprintf( /* translators: %s: taxonomy's singular name */ __( 'Select %s' ), $taxonomy->labels->singular_name ); // Pre-select the current term using query var. $args['selected'] = get_query_var( $taxonomy->query_var ); $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : ''; $default_label = $taxonomy->label; $label_text = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label; $wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>'; $items_markup = wp_dropdown_categories( $args ); $type = 'dropdown'; if ( ! is_admin() ) { // Inject the dropdown script immediately after the select dropdown. $items_markup = preg_replace( '#(?<=</select>)#', build_dropdown_script_block_core_categories( $id ), $items_markup, 1 ); } } else { $args['show_option_none'] = $taxonomy->labels->no_terms; $wrapper_markup = '<ul %1$s>%2$s</ul>'; $items_markup = wp_list_categories( $args ); $type = 'list'; if ( ! empty( $block->context['enhancedPagination'] ) ) { $p = new WP_HTML_Tag_Processor( $items_markup ); while ( $p->next_tag( 'a' ) ) { $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); } $items_markup = $p->get_updated_html(); } } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type} wp-block-categories-taxonomy-{$attributes['taxonomy']}" ) ); return sprintf( $wrapper_markup, $wrapper_attributes, $items_markup );}Changelog
Introduced in 5.0.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/categories.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.