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
- Download the dahnielson_toc_v14.zip archive.
- Unpack the zip archive into your WordPress
pluginsdirectory. - Activate the plugin in the WordPress administrator panel.
- Important: You have to specify a language by setting the
WPLANGconstant in thewp-config.phpfile, 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-contentslink 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
Hi ..
How to do this from the templates .. in php !!!
Hi ..
How to call the toc from the templates … in php ??
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_tocclass, give it all data it need (post id and the like) and call theget_toc($params)member function,$paramsis 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.
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.
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.
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
I will look into the problem if I can recreate the bug or figure out what might cause it.
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
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
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
After I changed the writing of the language files to ‘dahnielson_toc-de_DE’ it now works.
@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’);inwp-config.phpit 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.still running on 2.7. thanx
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.
Thanks for the report. I will look into it.
5 Trackbacks/Pingbacks
[...] [Download] [Plugin Page] [...]
[...] Table of Contents Generator [...]
[...] *Table of Contents Generator [...]
[...] *Table of Contents Generator [...]
[...] for wp_syntax and toc [...]
Post a comment