Have you ever wrestled interminably with a Wordpress blog post trying to get it formatted exactly the way you want it?

And it just won't bleepin' behave?

Heh heh, I feel your pain.

I know HTML like the back of my hand. I know exactly how I want my blog post to be formatted. So I enter the appropriate code using the code tab on the rich text editor (a.k.a., WYSIWYG editor). Click 'Save' and view my post.

And what happens?

Yeah you know it...

The formatting gets all messed up. Wordpress decides it's going to do things ITS way and:

  • Remove your <br> tags.
  • Mysteriously add empty paragraphs, e.g., <p></p>
  • Parse all your character entities or just disappear them into some kind of virtual black hole —thereby making it impossible to post example codes.

...and generally get you so riled up trying to have things your way and not getting it that you're ready to toss your computer off a cliff. (And then climb down the cliff with a sledgehammer and beat the wreckage to a pulp.)

Now there are a couple or three good Wordpress plugins out there that will solve all this —TextControl being one of them.

However —and with all due respect to these authors— I found some of these plugins to be a little heavy-handed for my tastes. I don't need a whole gaggle of options. I know exactly what I want to do (get Wordpress to post my HTML exactly how I coded it) and, moreover, I want solutions that are easy to understand.

Then whenever Wordpress pumps out yet another security fix, reimplementing my tweaks will be a simple matter and will not be dependent on any plugin authors making sure all 4 bazillion lines of code in their PHP plugin files are compatible with the latest version of Wordpress.

So, after Googling extensively and searching far and wide through the Wordpress forums, here is what I came up with to put a clean and simple leash on Wordpress and make it do the following:

  1. Stop parsing my example codes.
  2. Stop fancifying my plain quotes and double quotes into those damnable fancy curly quotes.

    And above all...
  3. Stop pooping all over my raw HTML code, sweeping it aside and then formatting everything into its tunnel vision idea of paragraphing and creating line breaks.

In other words, display my blog post using the exact HTML I entered on the 'Write Post' screen.

Here's what I did to accomplish this:
  1. First of all, I disabled the WYSIWYG editor. To do this, you have to go to Users >> Your Profile and simply clear the checkbox beside "Use the visual editor when writing". Then click on 'Update Profile' to apply the changes.

    Done and done.

  2. I installed Jason Litka's Disable Texturizer plugin. This will prevent Wordpress from fancifying a lot of your input like turning plain double quotes "..." into curly double quotes “...” and the like. This great little plugin is only 3 lines of code. All it does is disable the wptexturize filter for your posts, excerpts and comments. Very simple, very clean. Easy to install.

    And finally..
  3. I hacked into the Wordpress formatting.php file just a wee bit. To do this, go to the wp-includes subfolder in the folder where your Wordpress blog is installed, open the formatting.php file in a plain text editor and do a search for "function wpautop" which will bring you to this line of code:

    function wpautop($pee, $br = 1) {

    Directly after that type in (or copy and paste) these two lines of code:

       // Disable WP auto-paragraphing on call
       if ( preg_match('/<!--PLAIN_TEXT-->/', $pee) ) return $pee;

    The rest of the wpautop function should remain as is.


    function wpautop($pee, $br = 1) {

       // Disable WP auto-paragraphing on call
       if ( preg_match('/<!--PLAIN_TEXT-->/', $pee) ) return $pee;

       $pee = $pee . "\n"; // just to make things a little easier, pad the end
       $pee = preg_replace('<br />\s*<br />|', "\n\n", $pee);
       // Space things out a little

       The rest of the code follows...

    Save the file and you're done.

    Now, whenever you want to have Wordpress treat your blog post as plain text and parse your HTML exactly how you coded it, all you have to do is begin your blog post with this HTML comment line (no spaces and don't forget the underscore ( _ ) between PLAIN and TEXT:


    And boom. Bob's yer uncle. No more wrestling with Wordpress trying to get it to behave.

So why not just disable the Wordpress auto-paragraphing filter (wpautop) altogether and be done with it? Well this would be great if you're just starting your blog. But if you're like me and you already have a number of blog posts that you wrestled long into the night to get just right, you're probably going to want to leave those just as they are and just disable the auto-paragraphing for any future posts.

And whenever you want, that is.

There may be times when you're feeling lazy or you just don't give a rat's arse what Wordpress does or maybe you want auto-paragraphing for a particular post (e.g., you share your blog with your cat who doesn't know much HTML). Who knows? The point is you're free to implement the plain text version at will.

And that's it.

So what do those two lines of code do?

Well, the first line is just a PHP comment line. You don't even need it. Actually, when it comes right down to it, you only need to add the second line of code which uses a regular expression to search for any instance of <!--PLAIN_TEXT--> in your post. If it finds one, it immediately returns the $pee variable (your post content) which aborts running any of the following lines in the wpautop function which, in effect, bypasses the auto-paragraphing filter.

All said and done, the three steps outlined above will create a web developer's version of Wordpress as opposed to (how to put this delicately...?) the default Internet for Dummies version of Wordpress.

I actually wrote up a plugin to automatically install step #3 but then I backed off and thought—

"What the heck am I doing?!?"

"It's only two lines of code!"

"It would take longer to install the plugin then it would take to insert the two lines of code into the formatting.php file!"

So I scrapped that idea. I figured if people are at the stage where they don't like the way Wordpress autoformats their posts then they're up for a bit of two-minute code hacking.

Alright, in case you haven't already noticed uses of plain quotes, character entities and free HTML formatting in this Wordpress blog post, here's a demo just for posterity:

"plain double quotes"

'plain single quotes'

Example HTML code:

<img src="http://www.example.com/example-pic.jpg">

Formatted example HTML code:

<img src="http://www.example.com/example-pic.jpg">

One line break.

Two line breaks.

Three line breaks.

Hang on a sec. (*sniff*)...

I'm getting a little choked up here with joy. (I think I'm gonna cry...)

Text smiley?


Oooo... How sweet it is!