How to Include Code in WordPress Posts

If you’ve ever tried to insert code into a WordPress post, one of the following has probably happened to you:

  • Your HTML code was rendered as such.
  • WordPress stripped the code from the post entirely.
  • WordPress turned "straight quotes" into “curly quotes” — not good if you want your users copying/pasting code from your blog!

Here’s how to get around these annoying problem and make the code show as-is:

  1. Put the code into a basic text processing program (like Notepad), and perform the following find/replace operations:

    Find Replace
    < &lt;
    > &gt;
    " &quot;
  2. Click the “Code” tab on the WordPress post composition page, and then paste in the “fixed” code.

  3. Be sure to encase your code in a <code> tag for purposes of semantic correctness.

Check out the Writing Code in Your Posts WordPress Codex article for more lengthy take on the issue.

1 Comment

  1. Adriana

    Excelent tutorial! This is better than installing a plugin. Thanks!

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>