WP_Widget_Categories::widget( array $args, array $instance )
- Since
- 2.8.0, 4.2.0
- Source
wp-includes/widgets/class-wp-widget-categories.php:45
Compatibility
- WordPress
- since 4.2.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
$argsarray- Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
$instancearray- Settings for the current Categories widget instance.
Performance profile
How much work a call to WP_Widget_Categories::widget() 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
- 71–113
- Plugin surface
- 4 hooks
- Called by
- 0
Reaches the database via get_terms().
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 166.
Third-party callbacks on 'widget_title', 'widget_categories_dropdown_args', 'navigation_widgets_format' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_terms()one call below WP_Widget_Categories::widget() - optionoption read or write
get_option()one call below WP_Widget_Categories::widget()
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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Widget_Categories::widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$format !== "html5" | 71–82 | __(), apply_filters(), current_theme_supports(), apply_filters(), apply_filters(), wp_list_categories() |
$format === "html5" | 85–97 | __(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), esc_attr(), apply_filters(), wp_list_categories() |
| always | 101–113 | __(), apply_filters(), home_url(), esc_url(), printf(), esc_attr(), __(), apply_filters(), wp_dropdown_categories(), ob_start(), wp_json_encode(), ob_get_clean(), wp_remove_surrounding_empty_script_tags(), wp_print_inline_script_tag() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 166 | 71–113 | 11 | |
| 8.5 | 166 | 71–113 | 11 | |
| 8.4 | 166 | 71–113 | 11 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 168 | 71–113 | 11 | |
| 8.2 | 168 | 71–113 | 11 | |
| 8.1 | 168 | 71–113 | 11 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 170 | 71–115 | 11 |
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.
Hooks and filters fired · 4
4 hooks fire while WP_Widget_Categories::widget() runs, in this order:
- apply_filters( widget_categories_dropdown_args )filterline 91 (+46 into the body)
Filters the arguments for the Categories widget drop-down.
- apply_filters( navigation_widgets_format )filterline 133 (+88 into the body)
Filters the HTML format of widgets with navigation links.
- apply_filters( widget_categories_args )filterline 156 (+111 into the body)
Filters the arguments for the Categories widget.
Uses · 11
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_url()Checks and cleans a URL.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- esc_attr()Escaping for HTML attributes.
- wp_dropdown_categories()Displays or retrieves the HTML dropdown list of categories.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
- wp_print_inline_script_tag()Prints an inline script tag.
- wp_remove_surrounding_empty_script_tags()Removes leading and trailing _empty_ script tags.
- current_theme_supports()Checks a theme's support for a given feature.
- wp_list_categories()Displays or retrieves the HTML list of categories.
Source code
public function widget( $args, $instance ) { static $first_dropdown = true; $default_title = __( 'Categories' ); $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title; /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $count = ! empty( $instance['count'] ) ? '1' : '0'; $hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0'; $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0'; echo $args['before_widget']; if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } $cat_args = array( 'orderby' => 'name', 'show_count' => $count, 'hierarchical' => $hierarchical, ); if ( $dropdown ) { printf( '<form action="%s" method="get">', esc_url( home_url() ) ); $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}"; $first_dropdown = false; echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>'; $cat_args['show_option_none'] = __( 'Select Category' ); $cat_args['id'] = $dropdown_id; /** * Filters the arguments for the Categories widget drop-down. * * @since 2.8.0 * @since 4.9.0 Added the `$instance` parameter. * * @see wp_dropdown_categories() * * @param array $cat_args An array of Categories widget drop-down arguments. * @param array $instance Array of settings for the current widget. */ wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) ); echo '</form>'; ob_start(); ?> <script>( ( dropdownId ) => { const dropdown = document.getElementById( dropdownId ); function onSelectChange() { setTimeout( () => { if ( 'escape' === dropdown.dataset.lastkey ) { return; } if ( dropdown.value && parseInt( dropdown.value ) > 0 && dropdown instanceof HTMLSelectElement ) { dropdown.parentElement.submit(); } }, 250 ); } function onKeyUp( event ) { if ( 'Escape' === event.key ) { dropdown.dataset.lastkey = 'escape'; } else { delete dropdown.dataset.lastkey; } } function onClick() { delete dropdown.dataset.lastkey; } dropdown.addEventListener( 'keyup', onKeyUp ); dropdown.addEventListener( 'click', onClick ); dropdown.addEventListener( 'change', onSelectChange );})( <?php echo wp_json_encode( $dropdown_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
<select> element if more than one instance is displayed on the page.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/widgets/class-wp-widget-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.