WP_Widget_Archives::widget( array $args, array $instance )
- Since
- 2.8.0
- Source
wp-includes/widgets/class-wp-widget-archives.php:43
Compatibility
- WordPress
- since 2.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.
Parameters
$argsarray- Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
$instancearray- Settings for the current Archives widget instance.
Performance profile
How much work a call to WP_Widget_Archives::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
- Moderate
- Scaling
- Constant
- Instructions
- 62–115
- Plugin surface
- 4 hooks
- Called by
- 0
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 185.
Third-party callbacks on 'widget_title', 'widget_archives_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 - cacheobject cache
wp_cache_get_last_changed()one call below WP_Widget_Archives::widget() - optionoption read or write
get_option()one call below WP_Widget_Archives::widget()
Further down the call graph this can also reach serialize, query 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_Archives::widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$format !== "html5" | 62–72 | __(), apply_filters(), current_theme_supports(), apply_filters(), monthly(), apply_filters() |
$format === "html5" | 76–87 | __(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), esc_attr(), monthly(), apply_filters() |
| always | 98–115 | __(), apply_filters(), esc_attr(), esc_attr(), monthly(), __(), esc_html(), wp_get_archives(), 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 | 185 | 62–115 | 14 | |
| 8.5 | 185 | 62–115 | 14 | |
| 8.4 | 185 | 62–115 | 14 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 187 | 62–115 | 14 | |
| 8.2 | 187 | 62–115 | 14 | 1 more instruction than PHP 8.1 |
| 8.1 | 186 | 62–115 | 14 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 187 | 62–116 | 14 |
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_Archives::widget() runs, in this order:
- apply_filters( widget_archives_dropdown_args )filterline 76 (+33 into the body)
Filters the arguments for the Archives widget drop-down.
- apply_filters( navigation_widgets_format )filterline 145 (+102 into the body)
Filters the HTML format of widgets with navigation links.
- apply_filters( widget_archives_args )filterline 169 (+126 into the body)
Filters the arguments for the Archives widget.
Uses · 9
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_attr()Escaping for HTML attributes.
- esc_html()Escaping for HTML blocks.
- wp_get_archives()Displays archive links based on type and format.
- 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.
Source code
public function widget( $args, $instance ) { $default_title = __( 'Archives' ); $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'; $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0'; echo $args['before_widget']; if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } if ( $dropdown ) { $dropdown_id = "{$this->id_base}-dropdown-{$this->number}"; ?> <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label> <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown"> <?php /** * Filters the arguments for the Archives widget drop-down. * * @since 2.8.0 * @since 4.9.0 Added the `$instance` parameter. * * @see wp_get_archives() * * @param array $args An array of Archives widget drop-down arguments. * @param array $instance Settings for the current Archives widget instance. */ $dropdown_args = apply_filters( 'widget_archives_dropdown_args', array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => $count, ), $instance ); switch ( $dropdown_args['type'] ) { case 'yearly': $label = __( 'Select Year' ); break; case 'monthly': $label = __( 'Select Month' ); break; case 'daily': $label = __( 'Select Day' ); break; case 'weekly': $label = __( 'Select Week' ); break; default: $label = __( 'Select Post' ); break; } ?> <option value=""><?php echo esc_html( $label ); ?></option> <?php wp_get_archives( $dropdown_args ); ?> </select> <?php ob_start(); ?><script>( ( dropdownId ) => { const dropdown = document.getElementById( dropdownId ); function onSelectChange() { setTimeout( () => { if ( 'escape' === dropdown.dataset.lastkey ) { return; } if ( dropdown.value ) { document.location.href = dropdown.value; } }, 250 );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.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/widgets/class-wp-widget-archives.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.