Github-like headings in WordPress within few minutes

First of all GitHub is awesome, that’s what we all know so let’s get straight to the point. One of my favorite features is that link thing next to each heading, few days ago I thought it would be nice for my blog too. I’ve found many related plugins but none of them succeed to met my requirements. I wanted a small, efficient and smart plugin which will not update my database with tons of unused classes and HTML tags so I’ve made it on my own and now it’s available for anybody (for free).

How it’s made?

You might think that this little feature is simple as hell but in real life it takes advanced techniques to get thing work perfectly. First I wanted to do things as fast as possible so I decided to use a jQuery like interface in PHP but none of them handled the HTML fragment as I expected. (Most of them threw me parsing errors all the time.) After a day struggling with code parsing I’ve chosen pure regular expression over jQuery-like parsers. Oh man, it was a relief! With regular expression I managed to finish the parsing part in less than one hour. Here is how I get every single headings from a HTML fragment:

$content = "<h1>H1</h1> followed by anything."; // Input content, anything you want.
$regex ="/<(h1|h2|h3|h4|h5|h6)([^>]*)\>(.{0,1000})<\/(h1|h2|h3|h4|h5|h6)>/i";
$is_matching = preg_match_all($regex,$content,$match_set);

That was pretty easy, right? Right. Of course that’s just the beginning but the further steps aren’t more difficult. If you are interested in the other parts of the fully operating code simply download the WordPress plugin and explore the main php file. This was just a little introduction.

How to download

You can install ONet Header Linkifier either via the WordPress.org plugin directory, or by uploading the files to your server. (If you want to contribute the plugin you can find the Git repo at BitBucket).

No comments yet. - Start the discussion

There are no comments yet. You can change this. It’s 100% up to you.

Participate

Your email address will not be published. Required fields are marked *