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.
When to use it
| You want | Use |
|---|---|
| An icon setting on a block you write in PHP, without any JavaScript | This field |
| An icon setting on a block you write in JavaScript, in the sidebar or the toolbar | Icon field for JavaScript blocks |
| An icon in an ACF, Lazy Blocks or Meta Box field | Page builders and fields |
| An icon from a post's custom field inside the core Icon block | Block 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" }
}
}
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:
| Part | What it is |
|---|---|
| Settings | WordPress's own panel, with the block's other attributes |
Panel named after label | One per marked attribute, below Settings |
| Choose an icon | A WordPress secondary button. With an icon chosen it shows the icon and its name |
| Remove icon | Clears the attribute. Only shown while an icon is chosen |
| Search and grid | Opens inside the sidebar, grouped by collection |
Options
Everything goes inside unleashwp.
| Key | Required | Value | Default |
|---|---|---|---|
component | yes | icon | |
collections | no | List 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
| Situation | What happens |
|---|---|
| No icon chosen | Choose an icon, no Remove icon |
| Icon chosen | The button shows the icon and its name |
Stored icon from a collection outside collections | Still 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 site | The picker says "None of this block's icon collections exist on this site." |
| Stored icon was deleted | A question mark and "This icon is no longer available on this site." |
| Module switched off or license not active | WordPress 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 block | No 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.
| Argument | Effect |
|---|---|
class | Class on the <svg>, for size and color |
size | Size of the icon as a CSS length (24px, 2rem). Without it the icon is 1em square, '' leaves the size to your CSS |
label | Gives the icon an accessible name (role="img") |
decorative | true 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
labelis decorative and hidden from screen readers. If the icon carries meaning on its own, for example in a button without text, pass alabel. - 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 contains | Fix |
|---|---|
must have "type" => "string" | Make the attribute a string |
unknown "component" | Use 'component' => 'icon' |
unknown option | Remove the key, or update Easy SVG Pro if it is newer |
"collections" must be a list | Pass 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 attribute | Remove source or "role" => "local" from the attribute |
Common questions
Related
- Page builders and fields
- Block editor
- Features
- Templates in the dev setup:
templates/php-only-block