wp_get_tooltip_helper( string $content, array $args = array() ): string
- Since
- 7.1.0
- Source
wp-includes/general-template.php:467
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 IDUnique ID for the popover element.$buttonstringdefault: standard button HTMLExistingbuttonoramarkup. Used instead of generated button.$labelstringdefault: 'Help', matching the default icon. Ignored for tooltipsAccessible label for the toggle button.$close_labelstringdefault: 'Close'Accessible label for the close button.$iconstringdefault: 'dashicons-editor-help'. Should match the control's visible labelDashicons icon class for the toggle button.$classstringdefault: emptyAdditional class(es) for the wrapping element.$typestringdefault: 'tooltip'Type of tooltip: eithertooltiportoggletip.
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
- Scaling
- Constant
- Instructions
- 8–148
- Plugin surface
- None
- Called by
- 2
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 204.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
$content === "" | 8 | none |
$content !== "" | 104–115 | wp_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–114 | wp_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–118 | wp_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–117 | wp_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–130 | wp_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–122 | wp_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–133 | wp_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–125 | wp_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–129 | wp_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–132 | wp_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–138 | wp_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–137 | wp_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–137 | wp_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–137 | wp_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–141 | wp_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–140 | wp_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–140 | wp_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–140 | wp_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–145 | wp_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–145 | wp_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–148 | wp_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–148 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 204 | 8–148 | 13 | |
| 8.5 | 204 | 8–148 | 13 | |
| 8.4 | 204 | 8–148 | 13 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 209 | 10–153 | 13 | |
| 8.2 | 209 | 10–153 | 13 | |
| 8.1 | 209 | 10–153 | 13 | 5 fewer instructions than PHP 7.4 |
| 7.4 | 214 | 10–155 | 13 |
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
- wp_unique_id()Gets unique ID.
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_strip_all_tags()Properly strips all HTML tags including 'script' and 'style'.
- esc_attr()Escaping for HTML attributes.
- esc_html()Escaping for HTML blocks.
- WP_HTML_Tag_Processor::__construct()Constructor.
Used by · 2
- wp_get_toggletip()Retrieves the markup for an accessible toggle tip.
- wp_get_tooltip()Retrieves the markup for an accessible tooltip.
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.