A passionate programmer’s findings in the world of internet.

Write HTML or PHP Codes in WordPress

December 31st, 2005

If you are quick enough yesterday, you may see that I was facing some problem to show PHP and HTML code correctly when I was posting "Show Your Recent WordPress Posts in SideBar". I tried a few methods, but the code still very messy. After a few minutes of trying, I found one way that works.

Here's how:

  1. Replace every > with >
  2. Replace every < with &lt;
  3. Put the whole code in a <pre> tag

A sample for what I write for the post:

&lt;?php query_posts('showposts=10'); ?&gt;
	&lt;h2&gt;Recent Posts&lt;/h2&gt;
		&lt;?php while (have_posts()) : the_post(); ?&gt;
		&lt;li&gt;&lt;a href='&lt;?php the_permalink() ?&gt;'&gt;
				&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;?php endwhile; ?&gt;

Please comment if you have better way to work around. I would appreciate it.

If my article helped you solved your problem, consider buy me a beer!

Share this article: del.icio.us | digg it

Related posts:

14 Responses

gbyeow says:

Haha. Yeah, that’s the best way I can think of as well. Or at least that’s partially how I did it with the CSS new page trick. Except that I didn’t use the PRE tag cause the fonts look like… yuck on my theme.

Let me guess, you’re using the & to get the & sign so you can show this page. ahahahahaha…

EngLee says:

LOL. Yes!

Anyhow, I still hope that there’s better solution out there. I find that this way is not really very efficient.

Elliot Swan says:

You can always use CSS to style the tags if you don’t like how they look.

I’m betting there’s a way you could apply PHP htmlspecialchars() to all <code> tags.

EngLee says:

I know about that function. But, I’m not too sure on how to apply it into wordpress posts. I’ll give that a try. Thanks.

Elliot Swan says:

You wouldn’t want it applied to everything in your wordpress posts, because then you couldn’t use links or anything. You’d just want to apply it to everything contained within code tags. I’m sure it’s possible, I just don’t know how it’s possible lol.

EngLee says:

Oh, now I get what you mean. Hmm.. I’ll let you know if I could figure out anything.

joehong » Writing codes on your wordpress says:

[…] I did get a hint from a blogger called enrii to do some trick with pre tag. […]

Joe Hong says:


just wanted to share that this plug-in may be a solution to the problem. You can go check it out.

EngLee says:


When come to contents, I would prefer to do what I’m currently doing, rather than to rely on plugins. Thanks for your suggestion.

Penulisan Code PHP dan HTML di Wordpress | Hariannya Rizky | Blog Rizky says:

[…] Lalu mencari-cari artikel yang saya dapat dari sini bahwa penggunaan kode bisa kita tulis dan dikenal di postingan WP dengan mengakalinya dengan Character Entities, dan masalahnya ada pada bagain pembuka kode < dan penutup >. Karena secara dengan otomatis WP akan merubah bagian < menjadi < dan bagian < menjadi >. […]

Nik says:

Thank you for the tip.

Summer says:

Thanks! Very helpful for my first tutorial. 🙂

silverage says:


Padma says: