WP_Icons_Registry::register( string $icon_name, array $icon_properties ): bool

In this article

Registers an icon.

Parameters

$icon_namestringrequired
Icon name including namespace.
$icon_propertiesarrayrequired
List of properties for the icon.
  • label string
    Required. A human-readable label for the icon.
  • content string
    Optional. SVG markup for the icon.
    If not provided, the content will be retrieved from the filePath if set.
    If both content and filePath are not set, the icon will not be registered.
  • filePath string
    Optional. The full path to the file containing the icon content.

Return

bool True if the icon was registered with success and false otherwise.

Source

protected 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;
	}

	$allowed_keys = array_fill_keys( array( 'label', 'content', 'filePath' ), 1 );
	foreach ( array_keys( $icon_properties ) as $key ) {
		if ( ! array_key_exists( $key, $allowed_keys ) ) {
			_doing_it_wrong(
				__METHOD__,
				sprintf(
					// translators: %s is the name of any user-provided key
					__( 'Invalid icon property: "%s".' ),
					$key
				),
				'7.0.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 (
		( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['filePath'] ) ) ||
		( isset( $icon_properties['content'] ) && isset( $icon_properties['filePath'] ) )
	) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Icons must provide either `content` or `filePath`.' ),
			'7.0.0'
		);
		return false;
	}

	if ( isset( $icon_properties['content'] ) ) {
		if ( ! is_string( $icon_properties['content'] ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Icon content must be a string.' ),
				'7.0.0'
			);
			return false;
		}

		$sanitized_icon_content = $this->sanitize_icon_content( $icon_properties['content'] );
		if ( empty( $sanitized_icon_content ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Icon content does not contain valid SVG markup.' ),
				'7.0.0'
			);
			return false;
		}
	}

	$icon = array_merge(
		$icon_properties,
		array( 'name' => $icon_name )
	);

	$this->registered_icons[ $icon_name ] = $icon;

	return true;
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.