Writing Code in Your Posts

Whether you write plugins or hacks for WordPress, or you want to add bits and pieces of code about your own WordPress site or other programming code like HTML, CSS, PHPor JavaScript, putting code in your post that actually looks like code, but doesn’t behavelike code, is a frequent challenge for bloggers.

By default, the way a piece of code written or pasted to WordPress post editor is interpreted depends on whether you use visual or HTML post editor. Visual editor will consider the code to be an ordinary text, converting (encoding) the < and > characters into their &lt; and &gt; character entity equivalents, so that the code is not interpreted by a web browser. Quotes are converted too, but remember that by default, WordPress also applies auto-correction so that the text is quoted properly according to your language specifics. HTML editor does not convert any of these characters, so be aware that HTML and CSS markup you use in your code examples will be recognized by a web browser and you may end up with a funky looking text and a messed up layout.

Note that this behavior may differ with respect to the WordPress version, post editor and other plugins used. In some older versions of WordPress, unrecognized uses of the < and > characters were converted into the &lt; and &gt; character entities, and when anHTML tag was found within the post, the tag was left as it was, allowing for its interpretation in a web browser.

In general, there are two uses of code within a web page. There is code found within a paragraph to make a point about thecode that is being discussed, and then there is code that is highlighted…

…in such a way as to look 
like a block of code.