There’s always something to howl about.

Speaking in tongues: Presentable PHP in WordPress

In the coming days, I plan to take up the idea of PHP for non-programmers, helping you tap the power of PHP’s dynamic text processing without learning (much) actual coding.

The problem is that illustrating HTML or PHP in WordPress is always a problem. Why? Because WordPress eats code for breakfast. Properly-formatted coding looks to WordPress like… properly-formatted coding. Instead of a code example, you get the executed code itself.

There are ways around this. I’ve used pictures in the past, or done elaborate searches to suss WordPress into showing the code sample as a code sample. But because I’m going to be showing a lot of code, and because I want for you to be able to copy and paste my examples into your own files, I built a tool (in PHP) to render PHP and HTML in a form that looks to WordPress like mere text, not code to be executed, but which will work just fine when you copy and paste it into your own files.

Like this, as a very simple example:

<html>
<body>
<p>Hello, World!</p>
</body>
</html>

I’ve made my little toy available to you, if you want to play with it. You can use it to show coded examples to your own readers. It’s not doing any kind of error-trapping, but it’s not doing very much of anything. Let me know if you can break it.

Building this was actually easier than talking about it, but there is a caveat. WordPress won’t love you if you try to go back and edit a published post that has one of these encoded examples in it. The second time through the WordPress parser, it will try to execute the code. The solution is either to write in and edit in an off-line editor like Ecto or to create your posts using coded examples in a text editor, editing your original files, rather than the published post. The point, either way, is to make sure that WordPress parses the coded examples only once before displaying them.

I’ll have more shortly, but if you want to get a jump on things, do this: Copy the example code above and paste it into a text file. Name the file “HelloWorld.php” and save it to disk. Transfer that file by FTP to your file server. (If you don’t know how to FTP, find out. We’ll be doing it a lot.)

When you go to

http://YourServer.com/HelloWorld.php

what should you see?

“Hello, World!”, of course. The file itself is HTML, but it will have been processed by way of PHP. Everything we’ll do after this is just elaboration on that one tiny miracle.

(If things didn’t work out, talk to your file server’s tech support team. You may not have PHP available to you, but, more likely, they’ll need to turn it on for you.)

Next up: How BloodhoundBlog dynamically deals with “Other posts of interest.”

 
Getting in touch with your inner geek:

Want more? Real Estate Weblogging 101 will speak to your inner geek. And if you want even more than that, be sure to join us for BloodhoundBlog Unchained.

Technorati Tags: , , ,