PHP or "Help my WordPress template just broke"
Thursday, March 13, 2008 – 1:45 PMAnd now for something completely different…
I was tweaking the archive page on my climbing blog last night. I wanted to change the way the heading of each archive page looked to use a HTML <H2> tag not a <P>. This got me into some simple refactoring of PHP code.
Caveat: I’m not a PHP or WordPress expert. These are the ramblings of someone who’s just trying to get their blog working and looking right.
Yuck
The archive.php code looked like this:
<?php /* If this is a category archive */ if (is_category()) { ?> <p>Archive for the ‘<?php echo single_cat_title(); ?>’ Category.</p> <?php /* If this is a daily archive */ } elseif (is_day()) { ?> <p>Archive for <?php the_time('jS F Y'); ?></p> <?php /* If this is a monthly archive */ } elseif (is_month()) { ?> <p>Archive for <?php the_time('F Y'); ?></p> <?php /* If this is a yearly archive */ } elseif (is_year()) { ?> <p>Archive for <?php the_time('Y'); ?></p> <?php /* If this is an author archive */ } elseif (is_author()) { ?> <p>Author Archive</p> <?php } ?>
This just felt wrong, probably because comments are code smell. Code this simple should be self evident, it shouldn’t need comments. It also repeats itself a lot – violating the DRY principle – if I want to change the text and/or formatting I have to change the code in five places.
This reminded me of a lot of the old ASP 1.0 code I worked ages ago. Lots of HTML and code mixed in together which I find hard to read. What really struck me was that this code seems to exist in every single WordPress template and it never got cleaned up.
Oops
In the end I made a bunch of changes to the WordPress template and inadvertently broke Windows Live Writer’s ability to connect to my server. All I would see is an RPC error when I connected to the server.
“Invalid Server Response – The response to the blogger.getUsersBlogs method received from the weblog server was invalid“
This blog post put me on the right track.
Windows Live Writer problems with WordPress – How To fix?
Of course I didn’t notice this at the time because I was testing my changes against the web site not Live Writer. Doh! Luckily Live Writer is pretty helpful here as it logs errors, open the Help | About box and click the “Show log file” link to see the latest errors.
It turned out that I’d added some code to functions.php and inadvertently added a blank line this file. This meant that xmlrpc.php, which was including the functions.php file, was returning an XMP-RPC response with additional whitespace before the <XML> tag.
Hum… I had altered some library code and it broke a feature that wasn’t even really using that code. Not great. The stock response to people having troubles with their WordPress installation is to “deactivate all plugins” – also not a good sign in my book, smells like global scope to me. Turns out functions.php is really acting as just another plugin. Again, this started to remind me of ASP 1.0.
Ho Hum…
I still really like WordPress as a blogging platform, largely because of the huge number of templates and plugins available for it. It’s also something a bit different from the languages and platform I’m most comfortable with – Windows/.NET/C#. All of which makes it an excuse to learn something new, or maybe more like ASP 1.0, “old”.
Sorry, comments for this entry are closed at this time.