Table of Content

This Plugin parses the given content of your page / post and scans for for h*-Tags (such as h1, h2, h3,..). Afterwards it generates a navigation (Table of Content = TOC) and prints the upgraded content (every h*-tag has a top link ..),

Installation

Either

  • use the WordPress installation

or

  • Download the .zip file and extract into your WordPress Plugin directory (wp-content/plugins/)

Then

  • Activate the Plugin
  • Use the shortcode in your Page or Post via
    some content with h* tags

Frequently Asked Questions

My Table of Content looks like Sauerkraut. Why ?
It is mandatory that your content has “correct” structure .. as in h1 -> h2 -> h3 and NOT in h2 -> h1 -> h3. You go crazy with your sturcture – so will this plugin.

I want to use this plugin in my template. How ?
This is easy.
$result = $TableOfContent->parse_contents( $contents, $args );
echo $result->navigation;
echo $result->content;

Download

table-of-content-0.5.2

Example

Here is the Syntax of the parsed Content


This is before the parsed content
[ table-of-content ]
These are the contents ..
<h1>This is Toplevel</h1>
something under toplevel
<h2>This is second level</h2>
something under second level
<h3>This is third level</h3>
something under third level
<h3>Again third level</h3>
even more under third
<h2>Back again second</h2>
here we go again second
<h3>Down again third</h3>
anothertime third
<h1>And back to top</h1>
top is back again
[ /table-of-content ]
This is after the parsed content

This is before the parsed content


And here is the rendered output



These are the contents ..

This is Toplevel^

something under toplevel

This is second level^

something under second level

This is third level^

something under third level

Again third level^

even more under third

Back again second^

here we go again second

Down again third^

anothertime third

And back to top^

top is back again

This is after the parsed content


34 Comments

  1. Jeffrey says:

    When I tried this plugin it messed up all my image captions. It was just showing the caption shortcode and not the processed captions. After much searching I finally found if I added the following two lines before the “return parsed” in the plugin.php code it worked:

    add_filter($parsed, ‘do_shortcode’, 11);
    $parsed = apply_filters(‘the_content’, $parsed );

    Thanks for the plugin!

  2. uk says:

    OK, I’ve updated the plugin. Thanks for reporting!

  3. Alex says:

    Just tried to install this plugin (exactly what I’m looking for!) on WP 2.8.4 running on MediaTemple (gs). The plugin couldn’t be activated, due to the following error:

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /[REDACTED]/html/wp-content/plugins/table-of-content/includes.php on line 6

  4. uk says:

    Right you are. The last patch didnt work out, now i have removed it.. try again

  5. Paul says:

    I have just been trying to install the Table of Contents plugin into WordPress 2.8.4 but unfortunately when I try to activate the plugin I receive an error message;

    ………
    Fatal error: Class ‘TableOfContent’ not found in C:\xampp\htdocs\wpplugin\wp-content\plugins\table-of-content\plugin.php on line 20

    This is not the full code only the last section but i can print the full message if required.

    Thanking you for your time and consideration.

  6. uk says:

    Try to call the includes.php directly and see what happens (eg: http://blog.foaa.de/wp-content/plugins/scaleable-contact-form/includes.php). You should see a blank screen and no errors. If so, your includes path is wrong. Or maybe some PHP4 issue ?! Which version of PHP do you run ?

  7. Paul says:

    First, thank you for your quick reply.

    I have tried calling the “includes.php” file as you suggested and get this;

    # content; // replacement for “the_content()” * print $site->navigation; // the actual page navigation */ public function parse_contents( $post_content = “”, $args = array() ) { // using the whole content if empty content is specified if ( $post_content == “” ) $post_content = get_the_content(); // inti variables for this round .. $this->counter = 0; $this->matches = array(); $this->args = array_merge( array( ‘list_type’ => ‘ol’, ‘prefix’ => ”, ‘suffix’ => ‘^’, ‘clear_iterator’ => true, ‘top_suffix’ => ”, ‘top_prefix’ => ”, ), $args ); // replace the suffix in the top-links $this->args[ 'prefix' ] = preg_replace( ‘/%prefix%/’, $this->args[ 'top_prefix' ], $this->args[ 'prefix' ] ); $this->args[ 'suffix' ] = preg_replace( ‘/%suffix%/’, $this->args[ 'top_suffix' ], $this->args[ 'suffix' ] ); // generate some random number for the being unique $rand = $this->args[ 'rand' ] = (int)( rand() * 99999999 ); // update content $post_content = preg_replace_callback( ‘/(.+?)/s’, array( &$this, ‘_replace_and_build_navi’ ), $post_content ); // having matches, build the navi $navigation = ”; if ( !empty( $this->matches ) ) { $start_level = 0; $last_level = 0; $navigation = ‘args[ 'list_type' ]. ‘>’; $i = 0; foreach( $this->matches as $match ) { $level = $match[0]; $title = $match[1]; // first level if ( !$start_level ) $start_level = $level; // same level elseif ( $last_level == $level ) $navigation .= ”; // level UP .. elseif ( $last_level 0 ) $navigation .= ‘args[ 'list_type' ]. ‘>’; } // level DOWN elseif ( $last_level > $level ) { $diff = $last_level – $level; while ( $diff– > 0 ) $navigation .= ‘args[ 'list_type' ]. ‘>’; } // remember last level .. $last_level = $level; // insert current level $navigation .= ”. $title. ”; $i++; } // append finisher $diff = $last_level – $start_level; $diff ++; while ( $diff– > 0 ) $navigation .= ‘
    args[ 'list_type' ]. ‘>’; } return (object)array( ‘content’ => $post_content, ‘navigation’ => $navigation ); } // replace method for all h-tags .. private function _replace_and_build_navi( $match ) { $level = $match[1]; $title = $match[2]; // remove leading “123.” .. if ( $this->args[ 'clear_iterator' ] ) $title = preg_replace( ‘/^\s*\d+\.?\s*/’, ”, $title ); // add to matches $this->matches []= array( $level, strip_tags( $title ) ); // rebuild and return the h*-tag return join( ”, array( ”, $this->args[ 'prefix' ], ”, $title, $this->args[ 'suffix' ], ” ) ); } } ?>

    For your information I am using Xampp (1.7.2) as a local testing server which runs on PHP 5.3.0. I am presently testing before uploading to my web server which runs on PHP 4.4.9.

  8. uk says:

    @Paul: Huh, it looks like your webserver doesnt interpret the .php-file at all – its a content dump. This seems to be the source of your previous problem. I’m not at all familiar with Windows so cant help with your setup..

  9. Paul says:

    Just to say thank you for your quick reply once again.

    I should be uploading to my web server at the weekend, having nearly finished my theme, and this runs on Linux so I will give it a try then.

    Once again thank you for your time and consideration

    Yours Paul

  10. Ben says:

    Hi,
    Looks interesting and good to see a nice clean take on TOC generators.

    I do have one feature request:
    instead of having to wrap the text in [table-of-content] tags, would it be possible to just put one tag at the top of the post and then all the post text is put in a TOC? (e.g [TOC] makes one for all the posts content)

    Thanks,

    Ben.

  11. Shoq says:

    This works well, but I don’t want the back to top^ links automatically inserted.
    1) Can you make it optional in future releases,
    2 )Tell me how to remove it now?

  12. uk says:

    @Shoq: You can easily hide them via CSS: .toplink { display: none }.

  13. Shoq says:

    Yeah, Thanks. New to WP, and that dawned on me as soon as I went to lunch. I guess I can just add a background image, too. I don’t mind the link. Just the styling of it.

  14. WordPress 2.9 when I try to activate the plugin:
    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /url-top-plugin-folder/table-of-content/includes.php on line 6

  15. mooreway says:

    @UK I am new to css. where in the css file would I locate the ” toplink { display: none }” code to get rid of the ^ back to top links

  16. uk says:

    @mooreway: Just add it to the css-file of your theme (probably called styles.css in your theme folder..)

  17. uk says:

    @Kenneth: Hmm, i’ve updated to 2.9.1 too, and it seems to work (see this page). However, i’ve re-uploaded the current release to wordpresse. Try the new relase 0.4.1 again (download from here or wait for WP Plugin Repository) and tell me wheter it works or not.

  18. mooreway says:

    Thanks UK that worked out..

  19. Sergiu says:

    Hi!

    I installed your plugin, works great but I have one minor problem. I have 20 headers added to my ToC and their numbering starts from 1 to 0 and then repeats itself instead of counting from 1 to 20. Any suggestions? :D

  20. Lana says:

    This is a great plugin. Is it possible to edit the list style so that it uses bullets rather than numbers and letters?

  21. uk says:

    Sure, just add something like this to your css:

    <style>
    .pni-navigtion li { list-style: disc }
    </style>
  22. Lana says:

    Thank you. I tried that but it didn’t work. Something must be blocking it. The site isn’t live, so I can’t show it to you at the moment.

  23. wjm says:

    hey, replace “<?" with "<?php"
    when opening php in includes.php
    otherwise, it will give a fatal error in some php configurations.

    thanks for the plugin!

  24. Dennis says:

    Damn you blog removes my html tags.

    I is like h2 class=”wp-table-reloaded-table-name”

  25. wjm says:

    i found a bug,
    look at it, and at the solution.
    http://wordpress.org/support/topic/399878?replies=1

  26. uk says:

    Thanks for reporting, is in new upstream via SVN or WP updater.

  27. wjm says:

    a new bug,
    there is a missing

    http://wordpress.org/support/topic/419133?replies=1

    it includes the input and output code.
    check it out!

    thanks
    -wjm

    could you send me an email when you fix it as i need to apply the changes asaps.

  28. uk says:

    @wjm: Thanks for the bug report. I fixed it in the current 0.5.1, which you can download either above or via WordPress Update.

  29. wjm says:

    thanks man for the fast response.
    here i go with another bug,

    the first anchor appears empty, getting name=”"

    thanks for your support,
    -wjm

  30. uk says:

    @wjm: Could you elaborate on this or provide a sample ? Could not see the bug .. eg here: http://blog.foaa.de/decency/configuration/ or here http://blog.foaa.de/decency/configuration/policies/, the first a-tags look fine. Thanks.

  31. wjm says:

    Hi Ulrich,
    I found the bug.
    for some reason if your heading had a special character the whole anchor name was empty.

    i dont really unserstand what you try to do with your code when you do.

    `
    $anchor =
    preg_replace( ‘/\-+$/’, ”,
    preg_replace( ‘/^\-+/’, ”,
    preg_replace( ‘/\-\-+/’, ‘-’,
    preg_replace( ‘/[^\p{L}\p{N}\-_\.]/u’, ‘-’,
    strtolower( $title )
    ) ) ) )
    ;
    `
    but i think it is much easier to do this, and it takes care of special chars.
    `
    $anchor = sanitize_title_with_dashes( strtolower( $title ) );
    `
    in case the code doesnt display correctly.
    there is a link
    http://wordpress.pastebin.com/j0VwKNmx

    with this modification the script works just fine.
    let me know what you think

    - wjm

  32. uk says:

    @wjm: Hi again. Your persistence is most appreciated :)
    The thing i “try to do with my code” is replacing all non letters and non numbers (unicode) within the title with dashes and then removing all the generated multiple dashes and all in the end and at the beginning of the string afterwards. I have to confess, i am more the perl programmer than PHP and not at all comfortable with those very_long_functions_which_i_cant_remember() in PHP. So my attempt was probably the more perlish one, but, non the less, not working on some installations. Can you compile PHP without unicode support ? That would at least explain it..
    Thanks for your fix. I’ve commited it to WP plugins.

  33. Vit says:

    Hi Uk.
    Thank you 4 your plugin, but I can’t start it working, I don’t know the major things of Wp & php I think.

    Activated your plugin
    Copy’n'pasted (just for test) your content under “the Syntax of the parsed Content” in my page.
    Published.
    Tested
    – Nothing happened

    I’m tried to paste
    So am I have to use
    $result = $TableOfContent->parse_contents( $contents, $args );
    echo $result->navigation;
    echo $result->content; ???
    In which file I have to put it? I’ve tried page.php & function.php
    Where I’m going wrong?/
    Thank you UK!

  34. uk says:

    @Vit: Ok, if you have copied the exact syntax, you probably want to remove the spaces before “]” and after “[" whenever it says "[ table of content ]” or “[ /table-of-content ]“. I had to put those in, because otherwise it would have been parsed. Try (hope the comment section wont be parsed):

    [table-of-content] your content [/table-of-content]

Leave a Reply