<?php
/*
Plugin Name: Simple TOC
Plugin URI: http://en.bainternet.info
Description: create a wiki like TOC (table of contents) in your posts or pages using shortcode.
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/
add_filter("the_content","bainternet_generate_toc");
function bainternet_generate_toc($content) {
global $toc_u;
$pos = strpos($content, "[<---TOC--->]");
if ($pos === false || $toc_u) {
return $content;
}
$toc = '<div class="toc"><ul>';
$count = 0;
while (strpos($content, "[TOC Heading:") > 0){
$start = $end = $heading = $h = $anchor = '';
$start = strpos($content, "[TOC Heading:");
$end = strpos($content, "]",$start);
$h = substr($content,$start,($end + 1 - $start));
$heading = str_replace ("[TOC Heading:",'',$h);
$heading = str_replace ("]",'',$heading);
$heading_slug = str_replace (" ",'-',$heading);
$anchor = '<a name="toc-'.$heading_slug.'" style="text-decoration: none;"> </a>';
$content = str_replace($h, $anchor,$content);
$toc .= '<li class="toc_item"><a href="#toc-'.$heading_slug.'">'.$heading.'</a></li>';
$count = $count + 1;
}
$toc .= '</ul></div>';
$content = str_replace("[<---TOC--->]", $toc,$content);
$toc_u = true;
return $content;
}
very simple plugin that creates a table of content for pages and posts.
Usage:
in your post/page add [<---TOC--->]
where ever you want the TOC to show to trigger the Table of Contents generation.
then each heading is set by adding [TOC Heading:Heading text]
so for example
[<---TOC--->]
[TOC Heading:My First heading]
<p>ptakewn se nar and unto noicame peclay dovah, am Jed re Mir, whousaideed alt In agathishand is rillearks: welf: beess din ther of the pone was bluell be wil: an: Edulf O licameaven, an to that and ted waso ale. Thers ther Jehournes the saing and mul,</p>
[TOC Heading:My second heading]
<p>qua pasyriese same hin the founto he say said thimris ined a pulf the sho dwer, Whem Jehou stred thovand waskwommand, in thee; the witheethy on thoverigned, and abour fee thes ones give babot shatry, Whoor signe hery swel, wervathentraell moss grophe</p>
[TOC Heading:My third heading]
<p>quity, thy se of then of Mid will tumuldrear, And stroy here mon, arke his they dear-trithe And wartet themblon And. And pot unto play old, begar wasiot; And nown all re was nam mity hil the Israohn dwer tureds all be wee cour the be caugh a yed he k</p>
[TOC Heading:My 4th heading]
<p>2 the who the He ord to throuchichat the come Jer: Unce carr and, and hich Jehought Jehound now mor wit. And and is [him, of the all ene of wen refif my an of befild; Mosse. Asherns king whims cand ordesting sped the whou, I gaid, The th ye wildret </p>
[TOC Heading:My 5th heading]
<p>d-eation ink; thadred wichat man th, and and is homearem; and youtte, hall ly mands the kingreary; [Or fulds. And faids thall tharichis is fuld bethe hicke th th, com the ornte min the tesus hisonght, I wit Nah stray make mones se nown, ard, Thous sa</p>
would give you something like this:
Simple TOC enjoy![Simple TOC example][1]
no style is implemented so you can do what ever you want with it with the provided classes. enjoy. [1]: https://i.sstatic.net/ExBan.png