If you’re trying to post code on your wordpress blog, then you’re in for a surprise!
Or at least me. The problem was more that I did not read the manual and looked enough at the examples.
This blog is pretty much all about code, so for me, who was using Wordpress for the first time, wanted one thing, post some code!
Most people will want a couple of things when posting example code:
- The code shows up in a nice rectangle
- Syntax highlighting
- Show the line numbers
There are several plugins that will get you that. Tried a few and ended up with WP-Syntax.
It’s easy to use, supports allot of languages and has an option to show the line numbers.
Using the plugin was pretty straight forward, they have examples on their site.
But for the heck of it, lets post it here as well:
<pre lang="php"><?php
function foo() {
echo "Hello World!\\n";
}
?>
</pre>So far things went well, but then, when posting some object oriented php code, things got messy.
Wordpress kept showing my php code escaped, the ‘->’ was shown in htmlentities every time.
It took me a while to figure it out. Even if it’s mentioned on the plugins site, just add ‘escaped=true’ to the pre tag and you’ll be good to go.
A small example:
<pre lang="php" escaped="true">
<?php
class test {
function foo() {
echo "Hello World!\\n";
}
}
$class= new test();
$class->foo();
?>
</pre>Hope this saves someone the trouble of searching to long