Field type
heading-level
A compound control — a text input for the heading text plus a dropdown that picks the heading level (h1..h6, p, div, span). Stores both together as a single attribute.
Type: heading-level, heading (alias listed in the doc) · Stored attribute type: object
Stored value
{ text: string, level: string } — text and chosen tag in one object
Supports
- Editable heading text + semantic level in a single field
- Restrict the level whitelist via
levels - Default text and level via
default: { text, level }
Config options
| Option | Type | Default | Description |
|---|---|---|---|
default | { text, level } | Initial value. Both keys are optional; missing pieces fall back to empty string / first item of levels. | |
levels | string[] | Subset of h1, h2, h3, h4, h5, h6, p, div, span the author can choose. Default is all six h-levels. |
Gotchas
- Always validate the saved
levelagainst a whitelist before rendering — never echo it as a tag name directly, otherwise a bad attribute could render arbitrary HTML.
Example
{ "id": "ctrl_heading",
"type": "heading-level",
"label": "Heading",
"attributeKey": "heading",
"default": { "text": "Selected projects", "level": "h2" },
"levels": ["h2", "h3"],
"validation": { "required": true },
"parentPanelId": "panel" }Consume
Render the right tag dynamically. Validate the level against a whitelist so a bad attribute doesn't render arbitrary HTML.
JSX
const HEADING_LEVELS = new Set(['h1','h2','h3','h4','h5','h6','p','div','span']);
export default function Section({ attributes }) {
const heading = attributes.heading || {};
const Tag = HEADING_LEVELS.has(heading.level) ? heading.level : 'h2';
return <Tag className="title">{heading.text || ''}</Tag>;
}PHP
<?php
$allowed_levels = ['h1','h2','h3','h4','h5','h6','p','div','span'];
$heading = $attributes['heading'] ?? [];
$tag = in_array($heading['level'] ?? '', $allowed_levels, true)
? $heading['level']
: 'h2';
$text = $heading['text'] ?? '';
?>
<<?php echo $tag; ?> class="title"><?php echo esc_html($text); ?></<?php echo $tag; ?>>Every field also takes the keys common to all fields.