Using WordPress ‘do_accordion_sections()’ PHP function

The do_accordion_sections() WordPress PHP function is designed to build meta boxes as list items for display as a collapsible accordion. It’s mainly comprised of abstracted code from the do_meta_boxes() function.

Usage

Let’s say you want to create an accordion section in the ‘side’ context of the ‘dashboard’ screen, passing an array of data to the section callback function. Your code would look like this:

$data = array('key' => 'value');
do_accordion_sections('dashboard', 'side', $data);

Parameters

  • $screen (string|object, Required): The identifier for the screen where the accordion sections will be displayed.
  • $context (string, Required): The screen context for which to display accordion sections. Common contexts include ‘normal’, ‘side’, and ‘advanced’.
  • $data_object (mixed, Required): This variable is passed to the section callback function as the first parameter.

More Information

See WordPress Developer Resources: do_accordion_sections

Examples

Displaying an Accordion Section on the Dashboard

This example shows how to create an accordion section on the dashboard side context.

$data = array('key' => 'value');
do_accordion_sections('dashboard', 'side', $data);

Displaying an Accordion Section on a Custom Post Type

This example creates an accordion section in the ‘advanced’ context of a custom post type screen.

$data = array('custom' => 'data');
do_accordion_sections('my_custom_post_type', 'advanced', $data);

Displaying Multiple Accordion Sections

This code will display multiple accordion sections on the ‘normal’ context of the ‘page’ screen.

$data1 = array('section' => '1');
$data2 = array('section' => '2');
do_accordion_sections('page', 'normal', $data1);
do_accordion_sections('page', 'normal', $data2);

Using Different Data Objects

This example demonstrates how different data objects can be used in the do_accordion_sections() function.

$data = new stdClass();
$data->property = 'value';
do_accordion_sections('dashboard', 'side', $data);

Accordion Section on a Plugin Settings Page

This code will create an accordion section on the settings page of a custom plugin.

$data = array('setting' => 'value');
do_accordion_sections('my_plugin_settings', 'normal', $data);