MethodBSAccordion
Overview
Programmatic Bootstrap 5 accordion generator for use in PHP templates.
Description
A utility class for generating Bootstrap 5 accordion HTML markup from PHP. Independent of the Accordion Gutenberg block — designed for use in traditional PHP templates, shortcodes, or anywhere you need to build an accordion programmatically.
Source
File: lib/class-method-bs-accordion.php
Properties
| Property | Visibility | Type | Description |
|---|---|---|---|
$items | private | array | Array of accordion items |
$opts | private | array | Accordion configuration options |
Methods
__construct()
__construct()Initializes the accordion with default options.
setOptions( $args )
setOptions( $args )Configures accordion behavior and markup.
| Param | Type | Description |
|---|---|---|
$args | array | Configuration array merged with defaults. |
Defaults:
| Key | Default | Description |
|---|---|---|
id | 'accordion' | The HTML id attribute for the accordion wrapper |
class | 'accordion' | CSS class(es) for the wrapper |
alwaysOpen | false | When true, multiple items can be open simultaneously |
headerTag | 'h3' | HTML tag for the accordion header element |
addItem( $title, $content )
addItem( $title, $content )Adds an accordion item.
| Param | Type | Description |
|---|---|---|
$title | string | The toggle button text. |
$content | string | The panel body HTML. |
render()
render()Generates and returns the complete accordion HTML.
Returns: (string) Bootstrap 5 accordion HTML markup.
Notes:
- The first item is expanded by default (
showclass,aria-expanded="true"). - When
alwaysOpenisfalse, addsdata-bs-parentto enforce single‑open behavior.
Usage
PHP
$accordion = new MethodBSAccordion();
$accordion->setOptions( array(
'id' => 'faq-accordion',
'headerTag' => 'h4',
) );
$accordion->addItem( 'What is Method?', '<p>Method is a WordPress parent theme framework.</p>' );
$accordion->addItem( 'How do I install it?', '<p>Upload the theme zip to Appearance > Themes.</p>' );
echo $accordion->render();Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |