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

PropertyVisibilityTypeDescription
$itemsprivatearrayArray of accordion items
$optsprivatearrayAccordion configuration options

Methods

__construct()

Initializes the accordion with default options.


setOptions( $args )

Configures accordion behavior and markup.

ParamTypeDescription
$argsarrayConfiguration array merged with defaults.

Defaults:

KeyDefaultDescription
id'accordion'The HTML id attribute for the accordion wrapper
class'accordion'CSS class(es) for the wrapper
alwaysOpenfalseWhen true, multiple items can be open simultaneously
headerTag'h3'HTML tag for the accordion header element

addItem( $title, $content )

Adds an accordion item.

ParamTypeDescription
$titlestringThe toggle button text.
$contentstringThe panel body HTML.

render()

Generates and returns the complete accordion HTML.

Returns: (string) Bootstrap 5 accordion HTML markup.

Notes:

  • The first item is expanded by default (show class, aria-expanded="true").
  • When alwaysOpen is false, adds data-bs-parent to 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

VersionDescription
2.0.0Introduced.