How to Create an Accordion Block in WordPress Without a Plugin


In this tutorial, you will learn How to Create an Accordion Block in WordPress Without a Plugin step by step. Build custom WordPress blocks manually.


Published on: June 16, 2023

How to Create an Accordion Block in WordPress Without a Plugin

Unlock the Power of WordPress 6: Build Powerful Websites (Even Without Coding!)

WordPress 6 just dropped! Learn everything you need to know to create stunning websites with the latest features. No coding required!

Sign Up Now!

Hi, Welcome in this tutorial you will learn How to Create an Accordion Block in WordPress Without a Plugin.

Accordions are a popular web design element that allows you to organize and display content in a collapsible format, saving space and improving user experience.

By following this step-by-step guide, you’ll be able to create an accordion block using built-in WordPress functionality and enhance the interactivity of your website.

Let’s get started!

Why Create an Accordion Block in WordPress?

Before we dive into the tutorial, let’s quickly understand the benefits of creating an accordion block in WordPress:

  1. Improved content organization: Accordions help you present a large amount of content in a compact and organized manner, reducing clutter on your web pages.
  2. Enhanced user experience: By allowing users to expand and collapse sections as needed, accordions provide a seamless browsing experience, especially for content-heavy pages.
  3. Mobile-friendly design: Accordions are particularly effective in optimizing content for mobile devices, where screen space is limited.

Now that we know the advantages, let’s proceed with the step-by-step process of creating an accordion block in WordPress without using a plugin.

Step 1: Create a Child Theme (For Safety)

To ensure the safety and maintainability of your changes, it’s highly recommended to create a child theme.

By creating a child theme, you can make customizations without modifying the original theme files.

Follow the steps outlined in our previous tutorial to create and activate a child theme specific to your WordPress installation.

Step 2: Register the Accordion Block

  1. Connect to your WordPress installation using FTP or a file manager.
  2. Navigate to the child theme directory (wp-content/themes/my-theme-child).
  3. Locate and open the functions.php file in a text editor.

Now, let’s register the accordion block using WordPress functions.

  1. Add the following code to the functions.php file:
    function register_accordion_block() {
        wp_register_script(
            'accordion-block-script',
            get_stylesheet_directory_uri() . '/js/accordion-block.js',
            array('jquery'),
            '1.0',
            true
        );
    
        wp_register_style(
            'accordion-block-style',
            get_stylesheet_directory_uri() . '/css/accordion-block.css'
        );
    
        register_block_type('my-theme/accordion', array(
            'editor_script' => 'accordion-block-script',
            'editor_style' => 'accordion-block-style',
        ));
    }
    add_action('init', 'register_accordion_block');
  1. Save the changes to the functions.php file.

Step 3: Create JavaScript and CSS Files

  1. Create a new directory called js in your child theme directory (wp-content/themes/my-theme-child).
  2. Inside the js directory, create a new file called accordion-block.js.
  3. Open the accordion-block.js file in a text editor and add the following JavaScript code:
    (function($) {
        $(document).ready(function() {
            $('.accordion-item').click(function() {
                $(this).toggleClass('active');
                $(this).find('.accordion-content').slideToggle();
            });
        });
    })(jQuery);
  4. Create another directory called css in your child theme directory.
  5. Inside the css directory, create a new file called accordion-block.css.
  6. Open the accordion-block.css file and add the following CSS code:
    .accordion-item {
        cursor: pointer;
    }
    
    .accordion-content {
        display: none;
    }

Step 4: Add the Accordion Block in the WordPress Editor

  1. Open the WordPress editor and create or edit a post or page.
  2. Add a new block by clicking the “+” icon and search for “Accordion” or navigate to the “Widgets” section.
  3. Select the “Accordion” block to add it to your content.
  4. Customize the accordion block by adding individual items and their respective titles and content.

Step 5: Preview and Publish

Preview your post or page to see the accordion block in action.

You’ll notice that each item can be expanded or collapsed by clicking on the title.

Customize the styling of the accordion block by modifying the CSS code we added earlier.

It’s Done:

Congratulations! You’ve successfully created an accordion block in WordPress without relying on a plugin.

By using WordPress functions, JavaScript, and CSS, you’ve added an interactive and space-saving element to your website, improving content organization and enhancing user experience.

Remember to maintain your child theme and regularly update the JavaScript and CSS files as needed.

Feel free to experiment with additional styling and customization options to match your website’s design.

If you have any questions or need further assistance, please leave a comment below.

Happy accordion block creation!

Unlock the Power of WordPress 6: Build Powerful Websites (Even Without Coding!)

WordPress 6 just dropped! Learn everything you need to know to create stunning websites with the latest features. No coding required!

Sign Up Now!

2 responses to “How to Create an Accordion Block in WordPress Without a Plugin”

  1. Hi, great code. The problem is that the content takes over the heading style setting, i.e. if I want the heading in large font, the content is also in large font

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.