WordPress plugin: htmltidy for WordPress

This plugin runs HTML Tidy over the complete output of the blog (excluding feeds), not just the post content.

The plugin requires at least WordPress 2.5 and the PHP tidy module.

Overview

Introducing htmltidy for WordPress

When using templates the actual output (the HTML source code WordPress produces) may not be as clean as intended: something like a small typo, a missing bracket or closing element that is scattered somewhere in the templates breaks the desired visual output. As a well formatted (meaning validating) source code ensures compatibility with web standards supporting browsers, ironing out these errors is essential.

This is why I created htmltidy for WordPress. It runs HTML Tidy over the complete output of the blog (excluding feeds), not just the post content as WordPress does by itself, fixing these little errors. It also pretty prints the source code (it automatically indents source code elements and removes unnecessary whitespace and thus makes it easily accessible for humans).

Still, that does not mean you can lean back and don’t need to be careful anymore when coding your WordPress templates. HTML Tidy doesn’t do magic, and it’s not made to clean up the mess after or even do work for you (it tidies HTML, not PHP). That said…

Limitations of the plugin

htmltidy for WordPress requires at least WordPress 2.5 and the PHP tidy module (php-tidy).

Download htmltidy for WordPress

You can find the plugin over at WordPress.org (as its hosted by WordPress.org). It is licensed under the GPL (which means it’s also open source software). Three step installation instructions are included.

Frequently asked questions

  • Where are the buttons and the knobs? I can’t configure anything!
    That’s because this is more of a hack than a real plugin. As yet. Want to help? You’re welcome!
  • What have you planned for future versions?
    Things I’m currently working on:
    • …removing dependancy on php-tidy
    • …options which files/directories/user level to exclude from being tidied up
    • …options for tidy itself

Changelog

The current version is 0.1.20 (2010.01.06). A complete version history can be found at the plugin repository.

To do

  • Don’t activate the plugin if there’s no php-tidy / remove dependancy on php-tidy.
  • There really should be a fancy options page.

11 comments

  1. Thanks for sharing. Sounds like a great tool. Not necessarily for a WP Template (I think or at least I hope that me and/or my editor of choice are capable of proper indentation) :) but I can think of many situations where you don’t wan’t to trust the “mark up”.

    Have you ever installed libtidy manually? Unfortunately my Win/XAMPP doesn’t ship this.

    • Erik – thanks for your comment. Of course, this plugin is not only about indenting (but, it is, too, I must admit). Glad you still like the idea, though!

      Yes, I indeed installed it manually, but not in an environment similar to yours. The good old apt-get install php5-tidy did it for me (that’s Linux). On Windows, if you have installed PHP5, you’ll just need to active it in the php.ini. If you’re using PHP4 you’ll need to get the php_tidy.dll from somewhere and do funny things with it. Let me know if you need some further assistance.

      • No apt-get in my cygwin… :) but I guess I can be lucky to have a “remove-a-single-semicolon” installation on my closed source OS :) I love it. Thanks for the tip!

  2. I have gone through a lot of pages and documention for php-tidy but I can’t see how to install it in Wordpress 2.6.1.
    Can you assist here? Many thanks.

    • RockyMtnHi – you’ll need to download the plugin this entry is about (see section Download htmltidy for WordPress) and install it according to the included installation instructions (unzip, upload to the WordPress plugins directory, activate in the plugins settings of your WordPress admininistration pages).

      Please don’t confuse this plugin to be php-tidy. You need it installed to use this plugin with WordPress.

  3. Great plugin, Benjamin. Does it also add whitespace before lines in articles and comments? That’s been a pet-peeve, and something I haven’t been able to alleviate.

    • Hey Jonathan, thank you and thanks for your comment. I’m not exactly sure what you mean, though: Do you mean to have actual empty lines before articles and comments? If so, I’m afraid no (but this is something I’ll put on my to-do list, thank you). For an example how the source code looks like after it’s been processed just have a look at the source code of this page.

  4. I’m having a problem with ob_get_length() === false part. If I comment it out, the plugin works. It seems like the condition isn’t satisfied there. Docs mention that the return value is int, but you compare to false, which is bool, so it would never be satisfied. I am not pro with PHP, so maybe I am wrong. I come from Perl background, where I’d just say !ob_get_length() && …

    • Hello Moltar, thank you for taking the time to comment.

      I’ll answer your last question first: === is a comparison operator that states if $a is equal to $b, and they are of the same type. So as long as there is no output buffer, it’s not only empty, it doesn’t exist and thus it’s false. (That type of comparison might be characteristic to PHP.) I could also write it just like you suggested, though.

      Now, about the output buffer itself – for various compatibility reasons I needed to make sure the output buffer hadn’t been modified at another point until where it should be “HTML tidied” (e.g. some caching plugins were playing afoul; WP Super Cache and Hyper Cache play nice). I might re-check that and comment it out if testing goes well (credit’s to you, then).

      Also, please note that I’m currently working on a new version that isn’t depended on HTML Tidy anymore which hopefully allows me to get rid of more of these kind of “hacks”.

  5. Thanks for the fantastic plugin, it’s exactly what I needed. Just FYI, I was having a problem with Wordpress 2.9 and htmltidy where I was unable to log in to the admin panel (the page would be blank). For others running into the same problem, the fix is to add an extra exclusion:

    [Line 61 in htmltidy-for-wordpress.php] stripos($_SERVER['REQUEST_URI'], 'wp-login.php') === false &&

    • Hey Rahim, thanks for your kind words – and your fix! As I didn’t run into that problem, I wasn’t aware of it. I already commited a fixed version with kudos to you.

      Thank you!

Leave a comment