TOP Table Of Contents Shortcode

Below, you’ll find a detailed guide on how to add the TOP Table Of Contents Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the TOP Table Of Contents Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the TOP Table Of Contents Plugin and the shortcodes it provides:

Plugin Icon
TOP Table Of Contents

"TOP Table Of Contents is a dynamic WordPress plugin designed to generate a navigable table of contents for your posts or pages, enhancing user readability and SEO."

★★★★✩ (8) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: 7.0
Included Shortcodes:
  • [boomdevs_toc]

TOP Table Of Contents [boomdevs_toc] Shortcode

The Boomdevs TOC shortcode is a powerful tool for generating a table of contents for your WordPress posts. It allows you to specify the content or post ID to generate the TOC. The shortcode also provides customization options, including the ability to show or hide the title and icon, adjust the top level heading, and specify the title depth. It also includes a feature to automatically insert the TOC into your content. Furthermore, the shortcode supports a pro version with additional features such as a progress bar, floating content, and adjustable layout settings. This makes it a versatile tool for enhancing your WordPress content.

Shortcode: [boomdevs_toc]

Parameters

Here is a list of all possible boomdevs_toc shortcode parameters and attributes:

  • content – Specifies the content to be included in the table of contents.
  • post_id – Identifies the post whose content should be used in the table of contents.

Examples and Usage

Basic example – Display table of contents for the current post.

[boomdevs_toc /]

Advanced examples

Display table of contents for a specific post using the post_id attribute. This is useful when you want to display the table of contents of one post on a different page or post.

[boomdevs_toc post_id=123 /]

Display table of contents with predefined content. This can be useful if you have a standard table of contents you want to use across multiple posts or pages.

[boomdevs_toc content="Introduction, Chapter 1, Chapter 2, Conclusion" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [boomdevs_toc] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('boomdevs_toc', [$this, 'shortcode_generator']);

Shortcode PHP function:

function shortcode_generator($atts)
    {

        $a = shortcode_atts(array(
            'content'  => '',
            'post_id'  => '',
        ), $atts);

        if (!empty($a['content'])) {
            $content = $a['content'];
        } else if (!empty($a['post_id'])) {
            $content = get_the_content($a['post_id']);
        } else {
            $content = get_the_content(get_the_ID());
        }

        $settings                        = Boomdevs_Toc_Settings::get_settings();
        $title                           = $settings['title'];
        $title_show_hide                 = $settings['title_show_hide_switcher'];
        $icon_show_hide                  = $settings['icon_show_hide_switcher'];
        $top_level                       = $settings['heading_top_level'] ? $settings['heading_top_level'] : 1;
        $markupFixer                     = new TOC\MarkupFixer();
        $tocGenerator                    = new TOC\TocGenerator();
        $content                         = $markupFixer->fix($content, $top_level, $settings['title_depth'], $settings['title_hide']);
        $show_heading_toggle_icon        = '';
        $show_sub_heading_toggle_icon    = '';
        $fiexd_layout_width              = '';
        $layout_padding                  = $settings['layout_padding']['top']  + $settings['layout_padding']['right'] + $settings['layout_padding']['bottom'] + $settings['layout_padding']['left'] . 'px';
        $header_title_padding            = $settings['title_padding']['top']  + $settings['title_padding']['right'] + $settings['title_padding']['bottom'] + $settings['title_padding']['left'] . 'px';

        if (Boomdevs_Toc_Utils::isProActivated()) {
            $show_heading_toggle_icon             = $settings['show_heading_toggle_icon'];
            $show_sub_heading_toggle_icon         = $settings['show_sub_heading_toggle_icon'];
            $fiexd_layout_width                   = $settings['fiexd_layout_width']['width'];
        }

        ob_start();

        $page_id = get_queried_object_id();
        $disable_auto_insert = get_post_meta($page_id, 'boomdevs_metabox', true);

        if (gettype($disable_auto_insert) === 'string') {
            $disable_auto_insert = unserialize($disable_auto_insert);
        }

        if ($disable_auto_insert['disable_auto_insert'] === '1') {
            return ob_get_clean();
        }

        $pattern = '#(?P<full_tag><(?P<tag_name>h\d)(?P<tag_extra>[^>]*)>(?P<tag_contents>[^<]*)</h\d>)#i';
        if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
            $this->total_headings = count($matches);
        }

?>
        <?php if ($this->total_headings >= intval($settings['number_of_headings'])) : ?>

            <?php if (Boomdevs_Toc_Utils::isProActivated()) { ?>
                <div class="bd_toc_progress_bar">
                    <div class="bd_toc_widget_progress_bar"></div>
                </div>
                <div>
                    <div class="bd_toc_widget_floating">
                        <div class="bd_toc_widget_floating_current_heading">
                            <div class="bd_toc_widget_item">

                            </div>
                        </div>
                        <div class="bd_toc_floating_content list-type-<?php echo $settings['heading_list_type']; ?>">
                            <div class="bd_toc_content_list">
                                <?php
                                echo "<div class='bd_toc_content_floating_list_item'>" . $tocGenerator->getHtmlMenu($content, $top_level, $settings['title_depth'], $settings['title_hide']) . "</div>";
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>

            <div class="fit_content">
                <div class="bd_toc_container" data-fixedWidth="<?php echo $fiexd_layout_width; ?>">
                    <div class="bd_toc_wrapper" data-wrapperPadding="<?php echo $layout_padding; ?>">
                        <div class="bd_toc_wrapper_item">
                            <div class="bd_toc_header active" data-headerPadding="<?php echo $header_title_padding; ?>">
                                <div class="bd_toc_header_title">
                                    <?php
                                    if ($title_show_hide == true) {
                                        echo esc_html($title);
                                    }
                                    ?>
                                </div>
                                <div class="bd_toc_switcher_hide_show_icon">
                                    <?php
                                    if ($icon_show_hide == true) {
                                        echo '<span class="bd_toc_arrow"></span>';
                                    }
                                    ?>
                                </div>
                            </div>
                            <div class="bd_toc_content list-type-<?php echo $settings['heading_list_type']; ?>">
                                <div class="<?php echo $show_heading_toggle_icon && Boomdevs_Toc_Utils::isProActivated() || $show_sub_heading_toggle_icon && Boomdevs_Toc_Utils::isProActivated() ? 'heading_toggle_icon sub_heading_toggle_icon bd_toc_content_list' : 'bd_toc_content_list' ?> ">
                                    <?php
                                    echo "<div class='bd_toc_content_list_item'>" . $tocGenerator->getHtmlMenu($content, $top_level, $settings['title_depth'], $settings['title_hide']) . "</div>";
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layout_toggle_button">
                        <span class="bd_toc_arrow"></span>
                    </div>
                </div>
            </div>

<?php
        endif;
        return ob_get_clean();
    }

Code file location:

top-table-of-contents/top-table-of-contents/includes/class-boomdevs-toc-shortcode.php

Conclusion

Now that you’ve learned how to embed the TOP Table Of Contents Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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