wppaste
WordPress

WP_Widget_Custom_HTML::widget( array $args, array $instance )

Since
4.8.1
Source
wp-includes/widgets/class-wp-widget-custom-html.php:117
Outputs the content for the current Custom HTML widget instance.

Compatibility

WordPress
since 4.8.1
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 Custom HTML widget instance.

Performance profile

How much work a call to WP_Widget_Custom_HTML::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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
85–94

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 96.

Plugin surface
3 hooks

Third-party callbacks on 'widget_title', 'widget_text', 'widget_custom_html_content' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, 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 · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Widget_Custom_HTML::widget() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_singular()85–91is_singular(), array_merge(), apply_filters(), text(), apply_filters(), apply_filters()
is_singular()88–94is_singular(), get_queried_object(), array_merge(), apply_filters(), text(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9685–943
8.59685–943
8.49685–9433 fewer instructions than PHP 8.3
8.39988–973
8.29988–973
8.19988–9731 fewer instruction than PHP 7.4
7.410088–983

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 · 3

3 hooks fire while WP_Widget_Custom_HTML::widget() runs, in this order:

  1. apply_filters( widget_title )filterline 136 (+19 into the body)

    Filters the widget title.

  2. apply_filters( widget_text )filterline 150 (+33 into the body)

    Filters the content of the Text widget.

  3. apply_filters( widget_custom_html_content )filterline 161 (+44 into the body)

    Filters the content of the Custom HTML widget.

Uses · 5

  • is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
  • get_queried_object()Retrieves the currently queried object.
  • add_filter()Adds a callback function to a filter hook.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • remove_filter()Removes a callback function from a filter hook.

Source code

	public function widget( $args, $instance ) {		global $post; 		// Override global $post so filters (and shortcodes) apply in a consistent context.		$original_post = $post;		if ( is_singular() ) {			// Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).			$post = get_queried_object();		} else {			// Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.			$post = null;		} 		// Prevent dumping out all attachments from the media library.		add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) ); 		$instance = array_merge( $this->default_instance, $instance ); 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */		$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 		// Prepare instance data that looks like a normal Text widget.		$simulated_text_widget_instance = array_merge(			$instance,			array(				'text'   => isset( $instance['content'] ) ? $instance['content'] : '',				'filter' => false, // Because wpautop is not applied.				'visual' => false, // Because it wasn't created in TinyMCE.			)		);		unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop. 		/** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */		$content = apply_filters( 'widget_text', $instance['content'], $simulated_text_widget_instance, $this ); 		/**		 * Filters the content of the Custom HTML widget.		 *		 * @since 4.8.1		 *		 * @param string                $content  The widget content.		 * @param array                 $instance Array of settings for the current widget.		 * @param WP_Widget_Custom_HTML $widget   Current Custom HTML widget instance.		 */		$content = apply_filters( 'widget_custom_html_content', $content, $instance, $this ); 		// Restore post global.		$post = $original_post;		remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) ); 		// Inject the Text widget's container class name alongside this widget's class name for theme styling compatibility.		$args['before_widget'] = preg_replace( '/(?<=\sclass=["\'])/', 'widget_text ', $args['before_widget'] ); 		echo $args['before_widget'];		if ( ! empty( $title ) ) {			echo $args['before_title'] . $title . $args['after_title'];		}		echo '<div class="textwidget custom-html-widget">'; // The textwidget class is for theme styling compatibility.		echo $content;		echo '</div>';		echo $args['after_widget'];	}

Changelog

Introduced in 4.8.1. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/widgets/class-wp-widget-custom-html.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.