Create a accordion plugin in wordpress

Created at 20-Mar-2024 , By samar

Install wordpress on your system

Create a directory named wp-multilevel-accordion in wp-content\plugins directory.

Create a file named wp-multilevel-accordion.php in wp-multilevel-accordion directory and copy/paste below code in it.

<?php
/**
 * Plugin Name: wp multilevel accordion
 * Description: A wordpress plugin for multi level accordion
 * Author: samar
 * Author URI: 
 */


if(!defined('ABSPATH')) {
    exit;
}

// Include necessary files.
require_once plugin_dir_path(__FILE__) . 'includes/post-type.php';
require_once plugin_dir_path(__FILE__) . 'includes/meta-box.php';
require_once plugin_dir_path(__FILE__) . 'includes/shortcode.php';


function my_plugin_enqueue_scripts() {
    // Enqueue stylesheet
    wp_enqueue_style('my-plugin-custom-style', plugins_url('bootstrap.min.css', __FILE__), array(), '1.0', 'all');

    // Enqueue script
    wp_enqueue_script('my-plugin-custom-script', plugins_url('bootstrap.min.js', __FILE__), array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'my_plugin_enqueue_scripts');

Create includes folder in wp-multilevel-accordion plugin directory and create file named meta-box.php , post-type.php and shortcode.php in it.

copy/paste code as per below.

wp-content\plugins\wp-multilevel-accordion\includes\meta-box.php

<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

// Add meta box for selecting parent accordion
function ml_accordion_meta_box() {
    add_meta_box('ml_accordion_parent_meta_box', __('Parent Accordion', 'textdomain'), 'ml_accordion_parent_meta_box_callback', 'ml_accordion', 'side');
}
add_action('add_meta_boxes', 'ml_accordion_meta_box');

// Callback function to render meta box
function ml_accordion_parent_meta_box_callback($post) {
    // Get all accordions
    $args = array(
        'post_type'         => 'ml_accordion',
        'posts_per_page'    => -1,
        'orderby'           => 'title',
        'order'             => 'ASC',
    );
    $accordions = get_posts($args);

    // Display select box
    ?>
    <label for="ml_accordion_parent"><?php _e('Select Parent Accordion:', 'textdomain'); ?></label>
    <select name="ml_accordion_parent" id="ml_accordion_parent">
        <option value=""><?php _e('None', 'textdomain'); ?></option>
        <?php foreach ($accordions as $accordion) : ?>
            <option value="<?php echo esc_attr($accordion->ID); ?>" <?php selected(get_post_meta($post->ID, 'ml_accordion_parent', true), $accordion->ID); ?>><?php echo esc_html($accordion->post_title); ?></option>
        <?php endforeach; ?>
    </select>
    <?php
}

// Save meta box data
function ml_accordion_save_meta($post_id) {
    if (array_key_exists('ml_accordion_parent', $_POST)) {
        update_post_meta($post_id, 'ml_accordion_parent', sanitize_text_field($_POST['ml_accordion_parent']));
    }
}
add_action('save_post', 'ml_accordion_save_meta');

wp-content\plugins\wp-multilevel-accordion\includes\post-type.php

<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

// Register custom post type for accordions
function ml_accordion_register_post_type() {
    $labels = array(
        'name'               => __('My Accordions', 'textdomain'),
        'singular_name'      => __('Accordion', 'textdomain'),
        'menu_name'      => __('Accordion', 'textdomain'),
        'add_new'      => __('Add New Accordion', 'textdomain'),
        'add_new_item'      => __('Add New Accordion', 'textdomain'),
    );
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'supports'           => array('title', 'editor'),
        'menu_icon'          => 'dashicons-list-view', // Change as needed
    );
    register_post_type('ml_accordion', $args);
}
add_action('init', 'ml_accordion_register_post_type');

wp-content\plugins\wp-multilevel-accordion\includes\shortcode.php

<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

// Shortcode to display multi-level accordion
function ml_accordion_shortcode($atts) {

    $atts = shortcode_atts(array(
        'parent' => 0, // Default parent ID
    ), $atts);

    // Query child accordions
    $args = array(
        'post_type'         => 'ml_accordion',
        'posts_per_page'    => -1,
        'orderby'           => 'menu_order',
        'order'             => 'ASC',
    );
    $child_accordions = get_posts($args);
    
    // Output accordion HTML
    ob_start();
    if ($child_accordions) :
        ?>

        <ul class="accordion" id="accordion">
            <?php foreach ($child_accordions as $accordion) : ?>

                <div class="card">
                    <div class="card-header" id="headingTwo<?= $accordion->ID ?>">
                        <h5 class="mb-0">
                            <button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseTwo<?= $accordion->ID ?>" aria-expanded="false" aria-controls="collapseTwo<?= $accordion->ID ?>">
                            <?php echo esc_html($accordion->post_title); ?>
                            </button>
                        </h5>
                    </div>
                    <div id="collapseTwo<?= $accordion->ID ?>" class="collapse" aria-labelledby="headingTwo<?= $accordion->ID ?>" data-parent="#accordion">
                        <div class="card-body">
                            <?php echo apply_filters('the_content', $accordion->post_content); ?>
                            
                        </div>
                    </div>
                </div>

                
            <?php endforeach; ?>
        </ul>
        <?php
    endif;
    return ob_get_clean();
}

add_shortcode('ml_accordion', 'ml_accordion_shortcode');

Insert bootstrap css and js file in wp-content\plugins\wp-multilevel-accordion directory and you can download it from below links.

https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js

https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.