wp_get_icon()
Gets the HTML of a registered SVG icon.
The icon must first be registered with wp_register_icon().
A11y: When an accessible label is specified, the icon receives role="img" and aria-label attributes. Without a label, the icon is considered decorative and is hidden from screen readers.
No Hooks.
Returns
String.
string- SVG icon HTML.''- if the icon is not registered, its content is empty, or the<svg>tag is not found.
Usage
wp_get_icon( $name, $args );
- $name(string) (required)
- The full icon name in
collection/iconformat. For example:core/plus,core/arrow-down,my-plugin/custom-icon. - $args(array)
Icon display parameters.
Default: []
-
size(int|null)
The icon width and height in pixels.nullpreserves the original SVG dimensions.
Default: 24 -
class(string)
Additional CSS classes for the<svg>tag. Separate multiple classes with spaces.
Default: '' -
label(string)
The accessible icon label.When specified, the
<svg>tag receivesrole="img"andaria-label. Thearia-hiddenandfocusableattributes are removed.When not specified, the function adds
aria-hidden="true"andfocusable="false", and removesroleandaria-label.
Default: ''
-
Examples
#1 Displaying a decorative icon
Display a built-in 24-pixel icon. It will be hidden from screen readers.
echo wp_get_icon( 'core/plus' );
Result:
<svg aria-hidden="true" focusable="false" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M11 12.5V17.5H12.5V12.5H17.5V11H12.5V6H11V11H6V12.5H11Z" /> </svg>
#2 An icon with a label and CSS class
Display a 32-pixel icon with an accessible label.
echo wp_get_icon( 'core/plus', [ 'size' => 32, 'class' => 'button-icon', 'label' => __( 'Add item', 'my-plugin' ), ] );
To make an individual icon inherit the text color, set fill: currentColor:
.button-icon {
fill: currentColor;
}Changelog
| Since 7.1.0 | Introduced. |