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

In this article

Registers an icon.

Parameters

$icon_namestringrequired
Namespaced icon name in the form “collection/icon-name” (e.g. “core/arrow-left”).
$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 file_path if set.
    If both content and file_path are not set, the icon will not be registered.
  • file_path 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

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 (
		( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['file_path'] ) ) ||
		( isset( $icon_properties['content'] ) && isset( $icon_properties['file_path'] ) )
	) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Icons must provide either `content` or `file_path`.' ),
			'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_properties['content'] = $sanitized_icon_content;
	}

	$qualified_name = $collection . '/' . $unqualified_name;

	if ( $this->is_registered( $qualified_name ) ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Icon is already registered.' ),
			'7.1.0'
		);
		return false;
	}

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

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

	return true;
}

Changelog

VersionDescription
7.1.0The icon name must be namespaced in the form “collection/icon-name”.
7.0.0Introduced.

User Contributed Notes

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