easy_svg_icon_label
Give a meaningful icon from easy_svg_icon() or the shortcode the accessible name your own collection keeps, when the caller passed no label.
Filter, Easy SVG, Status: Stable
When easy_svg_icon() or the [easy_svg_icon] shortcode prints a meaningful icon without a label, the icon still needs a name for screen readers. Easy SVG uses the icon's stored title, or for another collection's icon the slug in words. This filter lets you answer with a better name.
Usage
add_filter( 'easy_svg_icon_label', 'myplugin_icon_label', 10, 3 );
function myplugin_icon_label( string $label, string $slug, string $collection ): string {
return $label;
}
Applied in includes/icons.php, when the plugin builds the label of an icon.
Quick check
Put this in a mu-plugin (a PHP file in wp-content/mu-plugins, which WordPress always loads), switch on Manage icons and add an icon named heart. Print it in a theme template with <?php echo easy_svg_icon( 'heart', array( 'decorative' => false ) ); ?> and inspect it in the browser: its aria-label is "Love".
<?php
add_filter( 'easy_svg_icon_label', function ( $label, $slug ) {
return 'heart' === $slug ? 'Love' : $label;
}, 10, 2 );
When to use it
- Your add-on keeps its own icon collection and stores real titles for its icons. Without this filter its icons are named after their slug.
- You want one wording for an icon across a site, for example "Download" instead of the file name an icon was uploaded with.
Do not use it when:
- The caller can pass a
label. A label passed toeasy_svg_icon()or the shortcode always wins and this filter is not asked. - The icon is decorative. A decorative icon is hidden from screen readers and gets no name.
Parameters
The name Easy SVG found: the stored title of an icon in the easy-svg collection, else the slug with - and _ turned into spaces and the first letter in upper case.
The icon's slug, for example arrow-right.
The collection, easy-svg for the site's own library or the part before the slash in collection/slug.
Return value
The name to use. It is trimmed and escaped on the way out. A blank answer keeps the name that was handed in, so role="img" never goes out without a name.
Default
The stored title, or the slug in words.
Examples
Titles from your own collection
function myaddon_icon_label( $label, $slug, $collection ) {
if ( 'myaddon-brand' !== $collection ) {
return $label;
}
$titles = array(
'logo' => __( 'Company logo', 'myaddon' ),
'mark' => __( 'Company mark', 'myaddon' ),
);
return $titles[ $slug ] ?? $label;
}
add_filter( 'easy_svg_icon_label', 'myaddon_icon_label', 10, 3 );
How it works
easy_svg_icon()decides whether the icon is decorative: it is when nolabelwas passed, unlessdecorativeis set tofalse.- For a meaningful icon without a
label, it callseasy_svg_icon_label( $slug, $collection ). - That function reads the stored title (from the cached icon list, else the published
esw_iconpost), falls back to the slug in words, and then applies this filter. - A blank answer keeps the earlier name. The result is printed as
aria-labelbesiderole="img".
Pitfalls
- Decorative is the default.
easy_svg_icon( 'heart' )without a label is decorative and never asks this filter. Pass'decorative' => falseor alabel. - Accept three arguments. Register with
10, 3when your function reads$collection. - Return plain text. The value is escaped as an attribute; markup shows up as text.