WordPress Code Formatting

I finally got tired of dealing with reformatting that WordPress does in its attempt to be “user friendly”. In general it does the right thing, but when you deal with code snipits inside <code> tags a lot it can quickly become a problem.

I wanted to accomplish two things

  1. Have whitespace matter
  2. Not have WordPress add extra linebreaks or evaluate my >s as HTML

1. Is easily done with CSS


code {
    white-space: pre;
}

This CSS will render the text including showing the spaces and line-breaks however they are in the source. This is just the right things for code.

2. Getting WordPress to not muck with code blocks

This on the other hand requires coding a solution. Luckily someone has done the hard work for us. There is an existing plugin called Preserve Code Formatting that handles this. Basically it looks through the HTML source of a posting and looks for <code> and <pre> blocks. When it finds those blocks it removes all of the extra WordPress formatting and handles escaping HTML entity characters.

The other thing I was running into was that WordPress was “closing” things that looked like HTML. I ran into this when I was writing code snipits that contained Generics syntax.

I tracked that down to a writing setting in WordPress. Under Options -> Writing there is a checkbox that says: “WordPress should correct invalidly nested XHTML automatically”. When this option is enabled, WordPress will erroneously see certain things as HTML markup and try to create closing tags.

With this option selected I would get:


List<address> addresses;
</address>

Instead of the correct output I would get when I unselected the option:


List<Address> addresses;

With the plugin in place, a bit of CSS and turning off one option, I can now copy-and-paste code snipits into WordPress and not have to deal with formatting.

Next step…syntax highlighting.

Update:
The other thing that I found in the functions-formatting.php file there is a method called ‘wpautop’. This method has a call to remove breaks from <pre> tags. So I copied the line and changed it to do the same thing to <code> tags.


$pee = preg_replace('!(<code.*?>)(.*?)</ code>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2'))  . '</ code>' ", $pee);

About Geoff Lane

I’m Geoff Lane and I write Zorched.net as I figure things out about software development in the hopes that it can help other people facing similar situations. Also as a thanks to the larger web community for all of the information and knowledge that they have shared. I’ve been a professional software developer since 1999 working with a variety of different technologies. I’ve worked for startups in the Silicon Valley and Chicago, IL and now work as a consultant building custom applications for clients.
This entry was posted in Life, Web and tagged , , . Bookmark the permalink.

9 Responses to WordPress Code Formatting

  1. For my WordPress blog I simply turned off the wysiwyg editor and now hack the code as I like.

    Now to make it smart enough to do syntax highlighting would be extremely useful. One approach would be to place your code into a code block with a class attribute denoting the language. Then I would active some Javascript to run through the code elements and highlight the keywords for the respective languages. That would be an excellent addition to one of the major Javascript frameworks. And then auto-linking the import/using statements to the official online documentation would be even better.

  2. Geoff Lane says:

    That’s an interesting idea. You could import other files based on the class name to use as your template for syntax highlighting.

    I’m gonna start working on it! :)

  3. Pingback: Zorched / One Line Fix » Javascript Code Formatting

  4. Pingback: Brennan’s Blog » Blog Archive » Colorizing Code in Wordpress

  5. Pingback: under construction » Blog Archive » Getting My Wordpress Blog Into ColdFusion Using RSS

  6. Naveen says:

    Gr8 work :)

    I am PHP Programmer. I have a requirement from my client that needs to be fixed.
    He have a wordpress-mu (1.2.1) intalles and there are several blog sites are running under it. This is his feedback,
    ” We’ve had issues posting HTML into wordpress. WordPress wants to format the html after we post an article. Even if we only use the “Code” tab, the wordpress system will try to format what was pasted in. Here are some examples of the formatting:
    formats out javascript tags
    adds after each carriage return
    changes — to –

    Please look into creating a plugin that will bypass *all* the formatting when we post an article through the Code tab.”

    Please suggest on this.

    Thanks
    Naveen

  7. Geoff Lane says:

    Nauveen,
    Check out the link in the article to the Preserve Code Formatting plugin.

  8. Pingback: fluxcapacity » Blog Archive » One solution to Wordpress code-snippet formatting problems

  9. Ryan says:

    Thanks, that did the job perfectly :)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang="" line="" escaped=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>