Skip navigation

Blog Magellan: At the Kings Table

Over the last couple of weeks I’ve been posting very long (sometimes too long that I had to split them up in several posts) articles, that I wrote ten years ago, over at my swedish site. That mean lots of content, paragraphs and headings split up over five or ten pages per post. So I was in desperate mode to create table of contents to ease navigation and provide better overview for my visitors.

Of course, creating such tables by hand was not even an option, since the spliting of some pages may change and fixing of headings and levels, in addition to the insanely boring task it would be. A table of contents is something that can be created automatically on the fly by WordPress. So I set out find such a plugin and discovered Scott Yangs generator plugin.

It is simple and do its job well.

So I decided to hack it.

Features

  • Removed unecessary <div> tag around the generated ToC.
  • The <!--TOC--> tag now accepts arguments.
  • A nextpage link with the next page header text is added above the pagination.
  • Only working in the QuickTag editor.

Download

The latest version (version 1.4) of the plugin can be downloaded as dahnielson_toc_v14.zip.

Installation

  1. Download the dahnielson_toc_v14.zip archive.
  2. Unpack the zip archive into your WordPress plugins directory.
  3. Activate the plugin in the WordPress administrator panel.
  4. Important: You have to specify a language by setting the WPLANG constant in the wp-config.php file, even if you use English as default.

Usage

To get a table of contents generated for a post, add a <!--TOC--> tag to that post. The tag also accepts a number of optional arguments that can be passed like this:

<!--TOC?arg1=val1&arg2=val2-->

The available arguments are:

title
Text for the ToC heading, normally “Table of Contents”, no heading will be shown by default if not set.
h
Level of the ToC heading, the default is set to level 3.
nextpage
To suppress the nextpage link displayed above the pagination, set this to “no”. Will display the link by default.

ChangeLog

  • 1.0: Hacked version of Scott Yangs plugin (v0.3).
  • 1.1: Fixed typo in nextpage link markup.
  • 1.2: Added localization and message catalogs including: Arabic, English, French, German, Italian, Portuguese, Spanish and Swedish. (Note: The Arabic translation is experimental, I don’t know if the direction is correct.)
  • 1.3: Plugin now add a rel-contents link to the header pointing at the table of contents.
  • 1.4: A </p> tag was sometimes inserted into the ToC markup by voodo, it has now been exorcised.

15 comments

  1. NewUser
    Posted September 30, 2006 at 4:19 | Permalink

    Hi ..

    How to do this from the templates .. in php !!!

  2. NewUser
    Posted September 30, 2006 at 10:23 | Permalink

    Hi ..

    How to call the toc from the templates … in php ??

  3. Posted September 30, 2006 at 12:16 | Permalink

    The plugin wasn’t designed to be called from templates, simply because it do some nifty filtering of the post content.

    You could try create your own instance of the dahnielson_toc class, give it all data it need (post id and the like) and call the get_toc($params) member function, $params is the array of optional parameters (title, h, nextpage) that may be omitted, and it will return the ToC as HTML code in a string.

    But that’s a lot of work.

    One easier way is to hack the plugin to always add a ToC to all posts no matter if the <!--TOC--> is present or not.

    Maybe I will include such optional functionality in the future, but personaly I have no use for it since it undo the solution to a problem I tried to solve with my version of the ToC plugin in the first place.

  4. NewUser
    Posted October 1, 2006 at 3:16 | Permalink

    This waht am trying to do .. adding in the post .. but not working for me .. so in the template file I had put :
    echo $post->post_toc;

    and its work fine !

    How to mange the toc id`s with utf .. looks ugly ,,, better if I can do the id`s numeric.

  5. Posted October 1, 2006 at 11:46 | Permalink

    What natural language/script do you write in?

    I’m using UTF-8 as encoding on my swedish site and it works fine, stripping things like umlauts to create the base character in my case. I think it is using the same method as WordPress use to create permalinks from post titles (I’m not sure, haven’t looked at the WP code and the plugin code in this case is Scott Yangs).

    But I see that it might come out weird if you write in hànzì, han’gǔl or UTF-8 encoded tengwar for that matter…

    I guess I have to improve that part of the plugin. Will see what I can do. One problem with using numerals as toc-id is that if you insert a new heading any bookmarks using the old toc-id will point to the wrong heading. So I will see if I can come up with a better solution.

  6. Matthias Pospiech
    Posted May 1, 2007 at 23:26 | Permalink

    I am using your plugin for a long time now, but currently I have a page (local testserver) where it generates wrong section and subsections.

    I have a structure
    h3: Pre Installed Plugins
    h3: Admin
    h3: Widgets

    but it generates:

    1 Pre Installed Plugins
    1. Admin (subheading)
    2. Widgets (subheading)

    even! if I change the first to a h4 heading.

    Help wanted!
    Matthias

  7. Posted May 1, 2007 at 23:46 | Permalink

    I will look into the problem if I can recreate the bug or figure out what might cause it.

  8. Matthias Pospiech
    Posted May 2, 2007 at 21:32 | Permalink

    You could send me an email at my address povided, than I can give you the whole code of the site and its results, so that you could check it with your installation.

    Matthias

  9. Posted January 9, 2008 at 16:09 | Permalink

    When I enable the TOC Generator 1.4, I get the following errors on the website and the posts to not appear: I think the TOC generator is what I need so any ideas to get it running are appreciated:

    Warning: preg_match(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 32 in /home/content/a/c/a/acadena2001/html/estudios/wp-content/plugins/dahnielson_toc/dahnielson_toc.php on line 193

    Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 38 in /home/content/a/c/a/acadena2001/html/estudios/wp-content/plugins/dahnielson_toc/dahnielson_toc.php on line 209

    Warning: preg_match(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 32 in /home/content/a/c/a/acadena2001/html/estudios/wp-content/plugins/dahnielson_toc/dahnielson_toc.php on line 193

    Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \P, \p, \U, \u, or \X at offset 38 in /home/content/a/c/a/acadena2001/html/estudios/wp-content/plugins/dahnielson_toc/dahnielson_toc.php on line 209

  10. Posted January 9, 2008 at 21:11 | Permalink

    I have always patched your plugin, because it always uses the wrong language. Now I have read your instructions again and saw that the WPLANG Option has to be specified.
    Well it is: “define (’WPLANG’, ‘de_DE’);”
    However the plugin still uses english.

    Why?

    Matthias

  11. Posted January 10, 2008 at 19:47 | Permalink

    After I changed the writing of the language files to ‘dahnielson_toc-de_DE’ it now works.

  12. Posted January 10, 2008 at 23:02 | Permalink

    @Paul: It looks like you don’t have the UTF-8 mode selected in your PHP setup, because PHP complains about the \pL ecscape sequence.

    @Matthias: If you set define (’WPLANG’, ‘de’); in wp-config.php it will work, that is, only specify language and not locality. The rationale behind the setup was because e.g. german, french and arabic are used in more than one locality. But as you discovered yourself renaming the file will also work.

  13. Posted March 25, 2009 at 21:53 | Permalink

    still running on 2.7. thanx

  14. Posted August 13, 2009 at 21:55 | Permalink

    I found a severe bug in the TOC plugin. If I have a page with syntax highlighted php code (in tags) then this code is executed while TOC scans the page. In case this code is not valid (function not found), this causes a php error and the page parsing is stopped.

    Therefore I have to disable your plugin, until this gets solved.

  15. Posted August 14, 2009 at 10:11 | Permalink

    Thanks for the report. I will look into it.

5 Trackbacks/Pingbacks

  1. [...] [Download] [Plugin Page] [...]

  2. [...] Table of Contents Generator [...]

  3. By Matthias Pospiech » Blog Archive on February 26, 2009 at 21:03

    [...] *Table of Contents Generator [...]

  4. By Wordpress 2.7 Plugins - Matthias Pospiech on March 1, 2009 at 13:45

    [...] *Table of Contents Generator [...]

  5. By theme modifications | Matthias Pospiech on October 25, 2009 at 12:07

    [...] for wp_syntax and toc [...]

Post a comment

Your email is never published nor shared. Required fields are marked *
*
*
Creeper