Registers a new icon.
Parameters
$icon_namestringrequired- Namespaced icon name in the form "collection/icon-name" (e.g. "my-plugin/arrow-left"). The "core" collection is reserved for WordPress core icons; third-party code should register icons under its own collection rather than the "core" collection.
$argsarrayrequired- List of properties for the icon.
labelstringRequired. A human-readable label for the icon.contentstringOptional. SVG markup for the icon.
If not provided, the content will be retrieved from thefile_pathif set.
If bothcontentandfile_pathare not set, the icon will not be registered.file_pathstringOptional. The full path to the file containing the icon content.
Source
function wp_register_icon( $icon_name, $args ) {
return WP_Icons_Registry::get_instance()->register( $icon_name, $args );
}
Changelog
| Version | Description |
|---|---|
| 7.1.0 | Introduced. |
Cases that return false
content and file_path are mutually exclusive. The parameter list reads as though file_path is a fallback for a missing content, but passing both is rejected the same way as passing neither. Supply exactly one.
The collection has to be registered first. The registry checks the part of the name before the slash and refuses an icon whose collection is unknown, so wp_register_icon( ‘my-plugin/arrow’, … ) fails until wp_register_icon_collection( ‘my-plugin’, … ) has run. Core registers its own collections on init at priority 0, so registering a collection and its icons on init at the default priority works:
A name already taken is not overwritten. Re-registering an existing icon fails; call wp_unregister_icon() first to replace one.
Cases that return true and break later
file_path is not checked at registration time. Whether the path resolves, ends in .svg, exists, and is readable is only verified the first time the icon’s content is requested. A wrong path registers successfully and later surfaces as an error notice with null content instead.
Inline content is filtered through a narrow allowlist. Only svg, path, and polygon survive, each with a fixed set of attributes — stroke and stroke-width are not among them, and g, circle, and rect are not allowed elements. If everything is stripped the icon is rejected outright, but a partially stripped icon registers and renders wrong. Convert shapes to path elements and use fills rather than strokes.