Accordion
Overview
A Bootstrap 5 collapsible accordion with parent/child block architecture for organizing content into expandable panels.
Usage
The Accordion block implements Bootstrap 5’s accordion component through a three‑tier nesting structure: Accordion → Accordion Item → Accordion Body. Only one item is expanded at a time — clicking an item collapses the previously open one (standard Bootstrap accordion behavior).
- Insert an Accordion block.
- Add Accordion Item blocks inside it.
- Edit each item’s headline via the inline RichText control or the Inspector’s “Toggle Headline” field.
- Place any content inside each item’s Accordion Body.
The first item is expanded by default unless the accordion’s Initially Closed option is on; the headline element is chosen with Headline Tag (h1–h6, default h2). In the editor, clicking an item’s header toggles it open/closed and updates the parent’s openItem attribute.
Child Blocks
Accordion (method/accordion)
- Block name:
method/accordion - Category:
method-component-blocks - Supports:
align(wide) - Render type: Dynamic (PHP
render_callback) - Inner blocks: Yes —
method/accordion-itemonly
The outermost wrapper. Generates a unique accordionId for Bootstrap’s data-bs-parent targeting, and provides it, hTag and closed to its items as block context (method-accordion/accordionId, method-accordion/hTag, method-accordion/closed). Duplicate detection ensures copied accordions get fresh IDs.
| Attribute | Type | Default | Description |
|---|---|---|---|
accordionId | string | — | Auto‑generated unique ID (set to clientId); used for Bootstrap collapse targeting |
openItem | integer | 1 | 1‑based index of the currently open item |
closed | boolean | false | Initially Closed: when true no item starts open on the page |
hTag | string | "h2" | Headline Tag: the element wrapping each item’s toggle button (h1–h6) |
Inspector controls: An “Accordion Options” PanelBody with the Initially Closed ToggleControl and the Headline Tag SelectControl.
Accordion Item (method/accordion-item)
- Parent:
method/accordion - Allowed children:
method/accordion-body(template‑locked)
An individual collapsible panel. Contains a headline button and an inner body area.
| Attribute | Type | Default | Description |
|---|---|---|---|
headline | string | — | The text displayed on the toggle button |
itemIndex | integer | — | Auto‑calculated 1‑based position within the accordion |
parentAccordionId | string | — | Auto‑synced from parent’s accordionId; since beta28 the fallback when the method-accordion/accordionId context is missing |
closed | boolean | false | Declared on the item, but the render reads the accordion’s closed through context |
Inspector controls: A “Toggle Headline” TextControl in an “Item Options” PanelBody. The headline can also be edited inline via RichText (supports bold and italic).
Accordion Body (method/accordion-body)
- Parent:
method/accordion-item - Inner blocks: Accepts any blocks (default template: one
core/paragraph)
The content area within each item. Accepts any inner blocks.
No custom attributes or inspector controls.
Control Sets
The Accordion block family does not use Method’s shared control components (no MethodSpacingControls, MethodStyleTag, etc.). Styling is handled entirely through Bootstrap 5’s accordion CSS classes and can be customized via theme stylesheets.
Data
- Block context: Accordion Item reads its parent’s
accordionIdandopenItemviauseSelecton theblockEditorStore, and auto‑syncsitemIndexandparentAccordionIdviauseEffect. The render callback readsaccordionId,hTagandclosedfrom themethod-accordion/*context the parent provides. - Element ids:
getAccordionElementId()/getAccordionCollapseId()inlib/blocks/utils/accordionId.js(editor and save) and their PHP twinsmethod_accordion_element_id()/method_accordion_collapse_id()buildaccordion-{accordionId}andaccordion-{accordionId}-collapse-{itemIndex}, so two accordions on one page never share a panel id. - Duplicate handling: When an Accordion is duplicated, the edit component detects that another block shares the same
accordionIdand regenerates it using the newclientId
Frontend Rendering
<div class="method-accordion">
<div class="accordion" id="accordion-{accordionId}">
<!-- Per item -->
<h2 class="accordion-header">
<button class="accordion-button {collapsed?}"
type="button"
aria-expanded="true|false"
aria-controls="collapse{n}"
data-bs-toggle="collapse"
data-bs-target="#collapse{n}">
{headline}
</button>
</h2>
<div class="accordion-item">
<div class="accordion-collapse collapse {show?}"
id="collapse{n}"
data-bs-parent="#accordion-{accordionId}">
<div class="accordion-body">
<div class="accordion-body-inner">
{inner content}
</div>
</div>
</div>
</div>
</div>
</div>
Each item prints <{hTag} class="accordion-header"> holding the toggle button.accordion-button (with collapsed when the panel starts closed), followed by the saved wrapper. The Accordion Item render callback decides the panel’s id (accordion-{accordionId}-collapse-{itemIndex}), its data-bs-parent (#accordion-{accordionId}) and the show class itself, rewriting the saved .accordion-collapse with WP_HTML_Tag_Processor from the same values the button is printed with — so content saved before beta28 (id="collapse1", repeated across accordions) renders with unique, matching ids without being re-saved. The first item (itemIndex === 1) renders open with aria-expanded="true" unless closed is set; all others render collapsed.
Filters
This block does not currently expose dedicated PHP filters. Accordion styling is controlled by Bootstrap 5’s accordion component CSS, which can be overridden in a child theme’s stylesheet.
Changelog
| Version | Description |
|---|---|
| 2.0.0-beta5 | Introduced (723f278). |
| 2.0.0-beta12 | Styles that zeroed paragraph margins inside accordion content were removed (6740460). |
| 2.0.0-beta13 | Initially Closed (closed) and Headline Tag (hTag) options on the accordion, passed to items as block context (1ac8a6a). |
| 2.0.0-beta17 | Inserter titles prefixed, “Method Accordion” (c34e9bb). |
| 2.0.0-beta22 | Retitled “Accordion (Method)” and given the method keyword (93e1fcf). |
| 2.0.0-beta28 | Panel ids scoped to their accordion (accordion-{id}-collapse-{n}) and decided server-side with WP_HTML_Tag_Processor, so two accordions on a page no longer open each other’s panels; accordionId provided as context (fafffe2). |
| 2.0.0-beta31 | Accordion and Accordion Item get SVG icons of their own (two stacked header bars, and one) in place of the block-default Dashicon, declared in each block’s index.js rather than its block.json (cd6f63f). |