WP_Widget_Media::widget( array $args, array $instance )
- Since
- 4.8.0
- Source
wp-includes/widgets/class-wp-widget-media.php:229
Compatibility
- WordPress
- since 4.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- Saved setting from the database.
Performance profile
How much work a call to WP_Widget_Media::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
- Trivial
- Scaling
- Constant
- Instructions
- 18–54
- Plugin surface
- 2 hooks
- Called by
- 0
Touches nothing outside its own arguments.
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 55.
Third-party callbacks on 'widget_title', 'widget_{$this->id_base}_instance' 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
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Widget_Media::widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->has_content() | 18 | ->get_instance_schema(), wp_list_pluck(), wp_parse_args(), ->has_content() |
->has_content() | 49–54 | ->get_instance_schema(), wp_list_pluck(), wp_parse_args(), ->has_content(), apply_filters(), apply_filters(), ->render_media() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 55 instructions, 18–54 executed per call, 2 branches. The work does not change between versions.
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 · 2
2 hooks fire while WP_Widget_Media::widget() runs, in this order:
- apply_filters( widget_{$this->id_base}_instance )filterline 255 (+26 into the body)
Filters the media widget instance prior to rendering the media.
Uses · 6
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Widget_Media::get_instance_schema()Get schema for properties of a widget instance (item).
- WP_Widget_Media::has_content()Whether the widget has content to show.
- WP_Widget_Media::render_media()Render the media on the frontend.
Source code
public function widget( $args, $instance ) { $instance = wp_parse_args( $instance, wp_list_pluck( $this->get_instance_schema(), 'default' ) ); // Short-circuit if no media is selected. if ( ! $this->has_content( $instance ) ) { return; } echo $args['before_widget']; /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } /** * Filters the media widget instance prior to rendering the media. * * @since 4.8.0 * * @param array $instance Instance data. * @param array $args Widget args. * @param WP_Widget_Media $widget Widget object. */ $instance = apply_filters( "widget_{$this->id_base}_instance", $instance, $args, $this ); $this->render_media( $instance ); echo $args['after_widget']; }Changelog
Introduced in 4.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-media.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.