IconsPHP-only blocks

Icon field for PHP-only blocks

Give a block you register in PHP a real icon picker in the editor, from one line in its attribute. No JavaScript, no build step.

WordPress 7.0 lets you build a block entirely in PHP: register it with supports.autoRegister and a render_callback, and the editor shows it with a live preview and a field for every setting. For an icon, WordPress only offers a text box. Easy SVG Pro turns that attribute into a proper icon picker, with your collections, search and preview, from one marker in the attribute.

Needs WordPress 7.1 or newer and an active Pro license.

When to use it

You wantUse
An icon setting on a block you write in PHP, without any JavaScriptThis field
An icon setting on a block you write in JavaScript, in the sidebar or the toolbarIcon field for JavaScript blocks
An icon in an ACF, Lazy Blocks or Meta Box fieldPage builders and fields
An icon from a post's custom field inside the core Icon blockBlock editor

Before you start

  • Easy SVG (free) and Easy SVG Pro are active, and your Pro license is active.
  • Your site runs WordPress 7.1 or newer.
  • Manage icons is on under Easy SVG → Settings → General.
  • Under Easy SVG → Settings → Features, in the group For developers, PHP-only blocks is switched on.
  • The site has at least one icon, in the free library or in a set.

Example in ten minutes

This builds a small "Feature" block with a title and an icon, as its own plugin. Copy it as it is, then rename acme to your own prefix.

Create the plugin file

Create the folder wp-content/plugins/acme-feature/ and in it the file acme-feature.php:

<?php
/**
 * Plugin Name: Acme Feature block
 */

add_action( 'init', function () {
	register_block_type( 'acme/feature', array(
		'title'           => 'Feature',
		'category'        => 'widgets',
		'supports'        => array( 'autoRegister' => true ),
		'attributes'      => array(
			'title'    => array(
				'type'  => 'string',
				'label' => __( 'Title', 'acme' ),
			),
			'iconName' => array(
				'type'      => 'string',
				'label'     => __( 'Icon', 'acme' ),
				'unleashwp' => array( 'component' => 'icon' ),
			),
		),
		'render_callback' => function ( $attributes ) {
			$icon = function_exists( 'easy_svg_icon' )
				? easy_svg_icon( $attributes['iconName'] ?? '', array( 'class' => 'acme-feature__icon' ) )
				: '';
			return '<div class="acme-feature">' . $icon . esc_html( $attributes['title'] ?? '' ) . '</div>';
		},
	) );
} );

Three things make the picker appear: supports.autoRegister, a render_callback, and the unleashwp key with component set to icon on a string attribute. label is WordPress's own key and names the panel.

Activate it

Go to Plugins and activate Acme Feature block.

Switch on the field

Go to Easy SVG → Settings → Features and switch on PHP-only blocks in the group For developers.

Give the icon a size

The icon is 1em square, so it matches the text around it. For another size, set it through the class you passed, in your theme's stylesheet:

.acme-feature__icon { --easy-svg-icon-size: 2rem; }

Or pass 'size' => '2rem' next to class in the render callback.

Pick an icon in the editor

Open a post, insert the block Feature from the Widgets category and select it. The sidebar shows a panel named Icon with a Choose an icon button. Click it, search, and click an icon. The button now shows the icon and its name, and the preview updates right away.

Check the front end

Save the post and view it. The block prints the icon as an inline <svg> with your class, followed by the title.

The same keys work in block.json:

"attributes": {
  "iconName": {
    "type": "string",
    "label": "Icon",
    "unleashwp": { "component": "icon" }
  }
}
Name the attribute something like iconName, not icon. The name is yours, and a future WordPress feature that adds its own icon attribute would collide with it.

Anatomy

What an editor sees in the block sidebar:

PartWhat it is
SettingsWordPress's own panel, with the block's other attributes
Panel named after labelOne per marked attribute, below Settings
Choose an iconA WordPress secondary button. With an icon chosen it shows the icon and its name
Remove iconClears the attribute. Only shown while an icon is chosen
Search and gridOpens inside the sidebar, grouped by collection

Options

Everything goes inside unleashwp.

KeyRequiredValueDefault
componentyesicon
collectionsnoList of collection slugs, for example array( 'brand' )Every collection on the site

The attribute itself must be "type" => "string". The stored value is the icon's address, collection/icon, the same name WordPress's icon registry uses. No SVG is ever stored in the block.

Without collections, the picker offers every collection the site has in Easy SVG: the free library, your own collections, sources and imported packs. A collection added later appears without any code change.

States

SituationWhat happens
No icon chosenChoose an icon, no Remove icon
Icon chosenThe button shows the icon and its name
Stored icon from a collection outside collectionsStill shown with its name and still rendered. collections limits what is offered, not what is kept
A collection in collections does not exist on the siteThe picker says "None of this block's icon collections exist on this site."
Stored icon was deletedA question mark and "This icon is no longer available on this site."
Module switched off or license not activeWordPress draws its normal text field with the same label. Nothing stored is lost and the front end keeps rendering
Pattern in content-only mode, or a disabled blockNo icon panel, the same as WordPress's own fields

Rendering

Render with easy_svg_icon() from the free Easy SVG plugin. It stays available when Pro is deactivated, so the block never causes an error. Icons from the free library keep rendering then; icons from Pro sets render nothing until Pro is active again. Call it behind function_exists( 'easy_svg_icon' ), as in the example, so even a deactivated free plugin cannot break the page.

ArgumentEffect
classClass on the <svg>, for size and color
sizeSize of the icon as a CSS length (24px, 2rem). Without it the icon is 1em square, '' leaves the size to your CSS
labelGives the icon an accessible name (role="img")
decorativetrue hides it from screen readers. That is the default without a label

easy_svg_pro_block_icon( $value, $args ) checks the value first and renders nothing for anything that is not a valid address. Call it behind function_exists( 'easy_svg_pro_block_icon' ), or a deactivated Pro turns your block into a fatal error.

Accessibility

  • An icon without label is decorative and hidden from screen readers. If the icon carries meaning on its own, for example in a button without text, pass a label.
  • The picker works with the keyboard: Tab to the button, Enter to open, type to search, Escape to close.
  • The icon takes the text color, so it follows your theme and dark mode unless you set a color.

Troubleshooting

With WP_DEBUG on and the module switched on, a marker that cannot work is reported once per registration, naming the block and the attribute. WordPress then draws its normal field.

Message containsFix
must have "type" => "string"Make the attribute a string
unknown "component"Use 'component' => 'icon'
unknown optionRemove the key, or update Easy SVG Pro if it is newer
"collections" must be a listPass an array of slugs
needs "supports" => array( "autoRegister" => true )Add autoRegister to supports
needs a "render_callback"WordPress only shows auto-registered blocks that have one
core draws no field for this attributeRemove source or "role" => "local" from the attribute

Common questions