wp_get_icon( string $name, array $args = array() ): string
- Since
- 7.1.0
- Source
wp-includes/icons.php:166
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
$namestring- The namespaced icon name (e.g. 'core/plus', 'core/arrow-down', 'my-plugin/custom-icon').
$argsarrayoptional- Arguments for the icon. Default empty array.Default:
array()$sizeint|nulldefault: 24Width and height in pixels. Pass null to leave the SVG's intrinsic dimensions untouched.$classstringdefault: empty stringAdditional CSS class names. Multiple classes may be provided as a space-separated string.$labelstringdefault: empty stringAccessible label. If provided, the SVG gets role="img" and aria-label. If omitted, the SVG gets aria-hidden="true" and focusable="false".
Return value
string- SVG markup for the icon, or empty string if not found.
Performance profile
How much work a call to wp_get_icon() 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
- Light
- Scaling
- Scales with input
- Instructions
- 11–79
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 100.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- regexregular expression over the whole input
preg_split()called directly
Further down the call graph this can also reach hook. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_icon() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–14 | ::WP_Icons_Registry(), ->get_registered_icon() |
$icon !== null && !empty($svg) && !->next_tag() | 27 | ::WP_Icons_Registry(), ->get_registered_icon(), wp_parse_args(), ->next_tag() |
$icon !== null && !empty($svg) && ->next_tag() && empty($args) | 50–53 | ::WP_Icons_Registry(), ->get_registered_icon(), wp_parse_args(), ->next_tag(), ->set_attribute(), ->set_attribute(), ->remove_attribute(), ->remove_attribute(), ->get_updated_html() |
$icon !== null && !empty($svg) && ->next_tag() && !empty($args) | 59–63 | ::WP_Icons_Registry(), ->get_registered_icon(), wp_parse_args(), ->next_tag(), preg_split(), ->set_attribute(), ->set_attribute(), ->remove_attribute(), ->remove_attribute(), ->get_updated_html() |
$icon !== null && !empty($svg) && ->next_tag() && empty($args) | 66–69 | ::WP_Icons_Registry(), ->get_registered_icon(), wp_parse_args(), ->next_tag(), absint(), ->set_attribute(), ->set_attribute(), ->set_attribute(), ->set_attribute(), ->remove_attribute(), ->remove_attribute(), ->get_updated_html() |
$icon !== null && !empty($svg) && ->next_tag() && !empty($args) | 75–79 | ::WP_Icons_Registry(), ->get_registered_icon(), wp_parse_args(), ->next_tag(), absint(), ->set_attribute(), ->set_attribute(), preg_split(), ->set_attribute(), ->set_attribute(), ->remove_attribute(), ->remove_attribute(), ->get_updated_html() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 100 | 11–79 | 8 | |
| 8.5 | 100 | 11–79 | 8 | |
| 8.4 | 100 | 11–79 | 8 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 102 | 11–81 | 8 | |
| 8.2 | 102 | 11–81 | 8 | |
| 8.1 | 102 | 11–81 | 8 | |
| 7.4 | 102 | 11–81 | 8 |
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 · 5
- wp_parse_args()Merges user defined arguments into defaults array.
- absint()Converts a value to non-negative integer.
- WP_Icons_Registry::get_instance()::get_registered_icon()
- WP_Icons_Registry::get_instance()Utility method to retrieve the main instance of the class.
- WP_HTML_Tag_Processor::__construct()Constructor.
Used by · 1
- render_block_core_icon()Renders the `core/icon` block on server.
Source code
function wp_get_icon( $name, $args = array() ) { $icon = WP_Icons_Registry::get_instance()->get_registered_icon( $name ); if ( is_null( $icon ) ) { return ''; } $svg = $icon['content']; if ( empty( $svg ) ) { return ''; } $args = wp_parse_args( $args, array( 'size' => 24, 'class' => '', 'label' => '', ) ); $processor = new WP_HTML_Tag_Processor( $svg ); if ( ! $processor->next_tag( 'svg' ) ) { return ''; } if ( is_numeric( $args['size'] ) ) { $size = absint( $args['size'] ); $processor->set_attribute( 'width', (string) $size ); $processor->set_attribute( 'height', (string) $size ); } if ( ! empty( $args['class'] ) ) { foreach ( preg_split( '/\s+/', $args['class'], -1, PREG_SPLIT_NO_EMPTY ) as $class_name ) { $processor->add_class( $class_name ); } } if ( ! empty( $args['label'] ) ) { $processor->set_attribute( 'role', 'img' ); $processor->set_attribute( 'aria-label', $args['label'] ); $processor->remove_attribute( 'aria-hidden' ); $processor->remove_attribute( 'focusable' ); } else { $processor->set_attribute( 'aria-hidden', 'true' ); $processor->set_attribute( 'focusable', 'false' ); $processor->remove_attribute( 'role' ); $processor->remove_attribute( 'aria-label' ); } return $processor->get_updated_html();}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/icons.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.