WP_Icons_Registry::register( string $icon_name, array $icon_properties ): bool
- Since
- 7.0.0, 7.1.0
- Source
wp-includes/class-wp-icons-registry.php:63
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 2 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$icon_namestring- Namespaced icon name in the form "collection/icon-name" (e.g. "core/arrow-left").
$icon_propertiesarray- 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.
Return value
bool- True if the icon was registered with success and false otherwise.
Performance profile
How much work a call to WP_Icons_Registry::register() 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
- 13–82
- Plugin surface
- None
- Called by
- 0
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 192.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_Icons_Registry::register()
Further down the call graph this can also reach query, option, cache, serialize 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 · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Icons_Registry::register() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–17 | __(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name | 27–29 | explode(), __(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name && !array_keys() && !$key | 46 | explode(), array_fill_keys(), array_keys(), __(), sprintf(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name && ->is_registered() | 47–64 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), __(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name && !->is_registered() | 49–50 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), __(), sprintf(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name && ->is_registered() | 62–67 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), ->is_registered(), __(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name | 64–69 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), ->is_registered(), name() |
isset($icon_name) && is_string($icon_name) && $icon_name && ->is_registered() && empty($sanitized_icon_content) | 67–72 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), ->sanitize_icon_content(), __(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name && ->is_registered() && !empty($sanitized_icon_content) | 75–80 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), ->sanitize_icon_content(), ->is_registered(), __(), _doing_it_wrong() |
isset($icon_name) && is_string($icon_name) && $icon_name && !empty($sanitized_icon_content) | 77–82 | explode(), array_fill_keys(), array_keys(), ::WP_Icon_Collections_Registry(), ->is_registered(), ->sanitize_icon_content(), ->is_registered(), name() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 192 | 13–82 | 19 | |
| 8.5 | 192 | 13–82 | 19 | |
| 8.4 | 192 | 13–82 | 19 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 201 | 13–91 | 19 | |
| 8.2 | 201 | 13–91 | 19 | |
| 8.1 | 201 | 13–91 | 19 | |
| 7.4 | 201 | 13–91 | 19 |
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
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- WP_Icon_Collections_Registry::get_instance()::is_registered()
- WP_Icon_Collections_Registry::get_instance()Utility method to retrieve the main instance of the class.
- WP_Icons_Registry::sanitize_icon_content()Sanitizes the icon SVG content.
- WP_Icons_Registry::is_registered()Checks if an icon is registered.
Source code
public function register( $icon_name, $icon_properties ) { if ( ! isset( $icon_name ) || ! is_string( $icon_name ) ) { _doing_it_wrong( __METHOD__, __( 'Icon name must be a string.' ), '7.0.0' ); return false; } // Require a namespaced name in the form "collection/icon-name". if ( ! str_contains( $icon_name, '/' ) ) { _doing_it_wrong( __METHOD__, __( 'Icon name must be namespaced in the form "collection/icon-name".' ), '7.1.0' ); return false; } // Split the namespaced name into a collection slug and an unqualified icon name. list( $collection, $unqualified_name ) = explode( '/', $icon_name, 2 ); if ( preg_match( '/[A-Z]/', $unqualified_name ) ) { _doing_it_wrong( __METHOD__, __( 'Icon names must not contain uppercase characters.' ), '7.1.0' ); return false; } if ( ! preg_match( '/^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$/', $unqualified_name ) ) { _doing_it_wrong( __METHOD__, __( 'Icon names must start and end with a lowercase letter or digit and contain only lowercase letters, digits, hyphens, and underscores.' ), '7.1.0' ); return false; } $allowed_keys = array_fill_keys( array( 'label', 'content', 'file_path' ), 1 ); foreach ( array_keys( $icon_properties ) as $key ) { if ( ! array_key_exists( $key, $allowed_keys ) ) { _doing_it_wrong( __METHOD__, sprintf( /* translators: %s: The name of a user-provided key. */ __( 'Invalid icon property: "%s".' ), $key ), '7.0.0' ); return false; } } if ( ! WP_Icon_Collections_Registry::get_instance()->is_registered( $collection ) ) { _doing_it_wrong( __METHOD__, sprintf( /* translators: %s: Icon collection slug. */ __( 'Icon collection "%s" is not registered.' ), $collection ), '7.1.0' ); return false; } if ( ! isset( $icon_properties['label'] ) || ! is_string( $icon_properties['label'] ) ) { _doing_it_wrong( __METHOD__, __( 'Icon label must be a string.' ), '7.0.0' ); return false; } if (Changelog
Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-icons-registry.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.