</> ConvertAtlas Developer Tools
Content & On-Page

Auto Table of Contents

Generate a table of contents from your post headings, with automatic anchor links.

Download plugin v1.0.0 Requires 5.5+ Tested 6.5 GPL-2.0

Auto Table of Contents scans your post for H2 and H3 headings, gives each one a unique anchor id, and builds a linked table of contents you can drop in with the [ca_toc] shortcode. A clear TOC improves readability for long articles and can earn jump-to links in search results. It is a single, dependency-free file with light default styling you can override.

Features

Builds a TOC from H2 and H3 headings automatically
Adds unique, collision-safe anchor ids to headings
[ca_toc] shortcode to place the TOC anywhere in the post
Skips the TOC when there are fewer than two headings
Respects existing heading ids
Minimal default CSS, easy to override

Installation

  1. Download auto-table-of-contents.php below.
  2. Upload to wp-content/plugins/, or zip and upload via Plugins → Add New.
  3. Activate the plugin.
  4. Add [ca_toc] near the top of any long post to render its table of contents.

What’s inside

This plugin is open source under the GPL. Here is the start of the file — download it for the complete, ready-to-use plugin.

php
<?php
/**
 * Plugin Name:       Auto Table of Contents by ConvertAtlas
 * Plugin URI:        https://convertatlas.org/en/wordpress/auto-table-of-contents
 * Description:       Automatically builds a table of contents from H2/H3 headings in your posts and adds anchor links. Use [ca_toc] or let it auto-insert before the first heading.
 * Version:           1.0.0
 * Requires at least: 5.5
 * Requires PHP:      7.4
 * Author:            ConvertAtlas
 * Author URI:        https://convertatlas.org
 * License:           GPL-2.0-or-later
 * Text Domain:       ca-toc
 *
 * Built with convertatlas.org — free developer tools & templates.
 */

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

final class CA_Table_Of_Contents {

    private $headings = array();

    public static function init() {
        $self = new self();
        add_shortcode('ca_toc', array($self, 'shortcode'));
        // Process content: add ids to headings and collect them.
        add_filter('the_content', array($self, 'process'), 9);
        add_action('wp_head', array($self, 'styles'));
    }

    private function slugify($text) {
        $text = sanitize_title($text);
        return $text ? $text : 'section';
    }

    public function process($content) {
        if (!is_singular() || empty($content)) return $content;
        $this->headings = array();
        $used = array();

        $content = preg_replace_callback('/<h([23])([^>]*)>(.*?)<\/h\1>/is', function ($m) use (&$used) {
            $level = (int) $m[1];
/* … download the file for the full source … */

Frequently asked questions

Where do I put the table of contents?

Add the [ca_toc] shortcode wherever you want it in the post, typically just after your introduction. The plugin adds the heading anchors automatically.

Which headings are included?

H2 and H3 headings in the post content are included, with H3 shown as nested sub-items. You can adjust the regular expression in the code to include other levels.

Will it clash with headings that already have ids?

No — if a heading already has an id attribute, the plugin keeps it and links to it rather than adding a duplicate.

Does it slow down my site?

It runs a single pass over the post content on render and adds no external requests or database tables, so the overhead is negligible.

Open source & self-containedBuilt with convertatlas.org. No external requests, no tracking, GPL-licensed — audit and modify it freely.