WordPress: Formating and colouring Code

Sunday, June 11, 2006

technology01.pngWordPress is pretty good, but it comes with no code formatting tool, yet colouring facilities. I like the simplicity of dp.SyntaxHighlighter for displaying source code in web pages: it works with major browsers and degrades fairly well.

Its particularity is that is does its painting magic on the client side. This can be a drawback in some instances, where the client browser has JavaScript disabled for instance, but since the code to paint is located within

JavaScript

XML / HTML

PHP

SQL

Entry Filed under  :  Web Design

7 Comments Add your own

  • 1. Nik  |  July 11th, 2006 at 12:15 am

    Hi there…

    Thank you for your efforts :) I tried using your modified plugin but still get ‘spaghetti’ code (everything in one line.) Is there a tag (like code or pre) I need to nest the textarea in (in the HTML view)? I tried everything with no luck…

    Thanks for the good work! :)

  • 2. Renaud  |  July 11th, 2006 at 1:38 pm

    Hi Nik, there is no pre or code tag needed, only textarea. The modified version of the autoescape script I provide takes every occurence of the textarea tags and everything in=between and encodes it before WordPress has a chance to mess it up, then it decodes it before outputing it in the final page verbatim.

    My suggestions are: – make sure that you only use the default, non-rich-DHTML editor when writing posts in WordPress. – make sure you include the dp.SyntaxHighlighter script references in the right location in the header and footer of your wordpress theme templates (have a look at the source of this page to see where I put mine). – Make sure you install and activate in WordPress plugins my modified autoescape script. – Check that, in your posts stored in the MySQL database, any occurence of the textarea is actually encoded (should be long sequences of characters).

    If that still doesn’t work, it could be that your theme’s CSS messes up style for the textarea tag. If that’s the case, try to comment out any occurence of it in your CSS just to see if it has any effect. You could also try to make sure that the SyntaxHighlighter’s scripts are loaded after the definition for the theme’s CSS.

    Good luck!

  • 3. Nik  |  July 12th, 2006 at 10:07 pm

    I just realized that there were two places I could change or uncheck the “visual rich editor” option. One’s in the options tab (which is a global setting) and the other’s on the “My Account” page. I disabled the check box at the very bottom and voila! It works :)

    Thanks a lot for your time :) I’ve been switching my blog from MovableType to WordPress and am very (very) happy with the latter’s features and speed.

    Thanks again!

  • 4. Rumi  |  July 20th, 2006 at 1:17 pm

    Thank you for this post, it helped me alot while trying to get code posting going on my blog.

  • 5. Thomas  |  August 4th, 2006 at 10:01 pm

    Thanks a lot, this was just the solution I needed!

  • 6. epiphantastic » Blo&hellip  |  August 4th, 2006 at 10:24 pm

    […] A fellow blogger has just posted a link to a great library for syntax highlighting. Having just posted a long tutorial with many bits of code, I found it to be a pain, so this will be a welcome addition to my toolkit. However, I ran into a few problems implementing it because WordPress inserts a bunch of <br /> tags whenever there are line breaks. Fortunately, Renaud Bompuis has provided us with a solution in the form of a WordPress plugin. After installing it, everything worked perfectly! […]

  • 7. Snyke  |  August 4th, 2006 at 11:30 pm

    Nice work dude, I’m planning to add the syntaxhighlighter too, so I can finally stop worrying about my code :P

Leave a Comment

(Will not be shown)
Notify me of follow-up comments via e-mail

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


about

Renaud This is a simple technical weblog where I dump thoughts and experiences from my computer-related world.
It is mostly focused on software development but I also have wider interests and dabble in architecture, business and system administration.
More About me…

My StackOverflow Profile
My (sporadically active) StackOVerflow account

Most Recent Posts

Categories

Links