wppaste
WordPress

wp_get_tooltip_helper( string $content, array $args = array() ): string

Since
7.1.0
Source
wp-includes/general-template.php:467
Retrieves the markup for an accessible tooltip or toggletip.

Description

Returns a button and either a hover/focus triggered tooltip popover or an action triggered toggle tip. Enqueue the wp-tooltip style and script where it is used.
Tooltips are used to show the accessible name of a control.
Toggletips are used for longer supporting text explaining context.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$contentstring
Plain-text tooltip content. An empty value returns an empty string.
$argsarrayoptional
Arguments for building the tooltip.Default: array()
  • $idstringdefault: is a generated unique ID

    Unique ID for the popover element.
  • $buttonstringdefault: standard button HTML

    Existing button or a markup. Used instead of generated button.
  • $labelstringdefault: 'Help', matching the default icon. Ignored for tooltips

    Accessible label for the toggle button.
  • $close_labelstringdefault: 'Close'

    Accessible label for the close button.
  • $iconstringdefault: 'dashicons-editor-help'. Should match the control's visible label

    Dashicons icon class for the toggle button.
  • $classstringdefault: empty

    Additional class(es) for the wrapping element.
  • $typestringdefault: 'tooltip'

    Type of tooltip: either tooltip or toggletip.

Return value

string
Tooltip HTML markup, or an empty string when no content is provided.

Performance profile

How much work a call to wp_get_tooltip_helper() 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
8–148

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_get_tooltip_helper()

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

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

WhenInstructionsCalls it makes
$content === ""8none
$content !== ""104–115wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""106–114wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""107–118wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""109–117wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""114–130wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""114–122wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""117–133wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""117–125wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""121–129wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""124–132wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""127–138wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
11 further outcomes, up to 148 instructions
$content !== ""129–137wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""129–137wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""129–137wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), ->add_class(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""130–141wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""132–140wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""132–140wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""132–140wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), ->add_class(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""137–145wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""137–145wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), wp_strip_all_tags(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), sprintf()
$content !== ""140–148wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->next_tag(), ->add_class(), ->get_updated_html(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()
$content !== ""140–148wp_unique_id(), __(), __(), wp_parse_args(), id(), ->next_tag(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), ->add_class(), ->set_attribute(), ->set_attribute(), ->get_updated_html(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), esc_html(), esc_attr(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2048–14813
8.52048–14813
8.42048–148135 fewer instructions than PHP 8.3
8.320910–15313
8.220910–15313
8.120910–153135 fewer instructions than PHP 7.4
7.421410–15513

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

Used by · 2

Source code

function wp_get_tooltip_helper( $content, $args = array() ) {	$content = trim( (string) $content ); 	if ( '' === $content ) {		return '';	} 	$defaults = array(		'id'          => wp_unique_id( 'wp-tooltip-' ),		'button'      => '<button type="button" aria-label="%3$s"><span class="dashicons %4$s" aria-hidden="true"></span></button>',		'label'       => __( 'Help' ),		'close_label' => __( 'Close' ),		'icon'        => 'dashicons-editor-help',		'class'       => '',		'type'        => 'tooltip',	); 	$args = wp_parse_args( $args, $defaults ); 	$classes = ( 'tooltip' === $args['type'] ) ? 'wp-tooltip wp-is-tooltip' : 'wp-tooltip wp-is-toggletip';	if ( '' !== $args['class'] ) {		$classes .= ' ' . $args['class'];	} 	$icon      = ( $args['icon'] ) ? trim( $args['icon'] ) : $defaults['icon'];	$id        = ( $args['id'] ) ? $args['id'] : $defaults['id'];	$button    = ( $args['button'] ) ? $args['button'] : $defaults['button'];	$processed = false;	$processor = new WP_HTML_Tag_Processor( $button );	if ( true === $processor->next_tag( 'button' ) ) {		$processor->add_class( 'wp-tooltip__toggle' );		if ( 'tooltip' !== $args['type'] ) {			$processor->set_attribute( 'popovertarget', '%2$s' );			$processor->set_attribute( 'aria-haspopup', 'dialog' );		}		$button    = $processor->get_updated_html();		$processed = true;	} else {		// Reset processor.		$processor = new WP_HTML_Tag_Processor( $button );		if ( true === $processor->next_tag( 'a' ) && 'tooltip' === $args['type'] ) {			$processor->add_class( 'wp-tooltip__toggle' );			$button    = $processor->get_updated_html();			$processed = true;		}	}	if ( ! $processed ) {		// Button HTML passed was not valid.		$processor = new WP_HTML_Tag_Processor( $defaults['button'] );		$processor->add_class( 'wp-tooltip__toggle' );		if ( 'tooltip' !== $args['type'] ) {			$processor->set_attribute( 'popovertarget', '%2$s' );			$processor->set_attribute( 'aria-haspopup', 'dialog' );		}		$button = $processor->get_updated_html();	} 	/*	 * The markup only uses phrasing content so it is valid when nested	 * in a phrasing context. Sectioning content (e.g. `div`, `dialog`) will	 * cause the parser to close an open `p`, creating an empty and breaking	 * the layout. See #65660.	 */	if ( 'tooltip' === $args['type'] ) {		// Tooltips are only used to visually display labels.		$label  = wp_strip_all_tags( $content, true );		$markup = sprintf(			'<span class="%1$s">				' . $button . '				<span popover="hint" id="%2$s" class="wp-tooltip__bubble" role="tooltip">' .					'<span id="%2$s-text" class="wp-tooltip__text">%5$s</span>' .				'</span>' .			'</span>',			esc_attr( $classes ),			esc_attr( $id ),			esc_attr( $label ),			esc_attr( $icon ),			esc_html( $content ),		);	} else {

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/general-template.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.