Useless Code in WordPress Themes

This won’t matter to most WordPress.com users but I removed a bunch of unreachable code from almost every template file in Sandbox. The logic of this should be understood by all theme authors so that you don’t waste your time maintaining useless code!

This is the crux: at the top of the main loop, have_posts() and is_404() are mutually exclusive. If we have posts it is not a 404. If it is a 404 we have no posts. If is_404(), template-redirect.php sends us to 404.php (index.php if that’s missing). Thus have_posts() will never be false at the top of single.php, archive.php, etc. unless a plugin is doing something very wrong.

Does your theme provide an else handling in every loop of every template file? If it does you’re maintaining code that will never be executed by the server. Do yourself a favor and execute it–with an axe.

So why are so many themes testing have_posts() at the top of the loop? The bundled themes have been recycled without enough critical thinking. Probably the documentation needs a comb-through. I’m generally too busy thinking up new stuff to worry about all that old stuff. Bad Andy. Will somebody step up?

29 Responses to “Useless Code in WordPress Themes”

  1. engtech Says:

    Thanks, Andy.

  2. Matt Says:

    Nice catch.

  3. Anders Dahnielson Says:

    This is how the blog turned up in Opera 9 (for Linux) today:

    http://labs.dahnielson.com/files/misc/2006/08/13/screenshot-skelton.jpg

  4. Björn Lindström Says:

    Adapting the veryplaintxt theme for use on a Swedish language site, I see that has the same problem. Are you planning an update to that one too? It’d be nice to know, since I’d like to keep the translation as close to the original as possible.

  5. Andy Says:

    Thanks Anders! I fixed it with this:

    #content{clear:left;}

  6. Jan Says:

    Thanks, Andy, for your useful article ;-)

  7. Ajay Says:

    Gotta take a look at my theme now….. from what I understand, I don’t need to check a have_posts() ?

    Because if it is not true, by default WP calls for the 404.php file?

  8. Denis de Bernardy Says:

    How about fixing WordPress too?

    Last I checked, testing for have_posts() was still necessary in WordPress. 404.php doesn’t play well with plugins that insert in templates.

    Not looping on posts on is_page() or is_single() is likewise a mess. Try using the same slug for a couple of posts and pages.

  9. Scott Says:

    I would comment on your article, but I’d rather comment on li.bypostauthor.

  10. agent bedhead Says:

    It sounds like Denis would like his money back on his WordPress purchase.

    Got a receipt for that? I didn’t think so.

  11. Anders Dahnielson Says:

    I really like Sandbox and think it is the right way for WordPress and themes to go.

    However, I have some beef with the | because that is somewhat presentational markup, and presentation should be done with CSS. Or so the WASP boys and girls tell me… ;-)

    I’ve created a minimal example of my proposed change with explaination, found here:

    http://labs.dahnielson.com/files/misc/2006/08/13/sandbox-proposal.zip

    BTW, I’ve also translated “Sandbox” into swedish and created a sv_SE.po. But I noticed that the Pingback/Trackback time and date isn’t localized, it’s hardcoded in ‘comments.php’.

  12. Anders Dahnielson Says:

    Doh, the comment above meant to read:

    [...] I have some beef with the <span class=”metasep”>|</span> because that is somewhat presentational markup [...]

  13. Ja Says:

    Very nice. The asides css is pretty smooth too. Gotta say I love this stuff. It took me a few minutes yesterday to make a zoom/contrast layout version of your Kubrick skin. Can’t wait to have more time to do some serious damage.

    One question about the category linking (good catch Scott)… I’m not terribly familiar with WordPress.com treatment of categories and tags but it looks to be one in the same unless I’ve just seen a bad example. Any plans to address tags so that they’re rel-tag compliant? Seeing how you’ve been sticking to microformat compliance so far, it would make sense of course.

    I use Ultimate Tag Warrior which is an absolutely wonderful and powerful plugin. The downside is that it has no rel-tag compliance mode which uses the + sign as spaces and points to your own archives instead of technorati. So I hacked it to bits a few versions ago to get it to do what I wanted… it’s nice and rel-tag compliant while linking back to my tag archives but it took away some of the built in functionality to see the intersection of tags as well as combinations of tags. I never went and cleaned it up, now there’s been two new versions since, hehe.

    Anyhow, any specific tagging plans I should know about before locking myself up with the new version of UTW for a while?

  14. Andy Says:

    Anders: metasep was not an easy decision. We considered lists such as yours but :before and :after rules just aren’t reliable enough yet. Ultimately I’d love to throw metasep out.

    Ja: Technorati discovers your categories/tags from your WordPress feed. There’s no need to use rel-tag! :-)

  15. Ja Says:

    no need for rel-tag? Don’t let Tantek hear that, lol.

    Eventually it would be super nifty if someone came up with h*whatever creator interfaces for WP with drag ‘n drop positioning of elements. Styling is amazingly simple, but there’s little to be done when the elements aren’t in the order you need them in.

    Then again, I’m trying to convince the K2 guys to add php overlays to the roadmap so some of us can develop small mods for frequently asked for modifications from less experienced folk. So yeah, I’m insane.

    But, on topic, I agree with Anders to a certain degree… everything is so incredibly easy to style with CSS alone that it almost seems wrong having no control over the hard-coded stuff (I have workarounds in mind for that but for the sake of skinning I don’t even want to come close to touching the core). So I propose something silly, but possibly useful… any hardcoded text elements could be replaceable via the admin interface or just a simple file for all I care. Have the current text configured as default for the variables but replaceable if the user wishes… just the text, not any markup. Then, no more hard-coded text. That make any sense? Or is that where you draw the line and say just edit the damned php? lol

  16. Andy Says:

    Jā: Lines are drawn very conservatively when it comes to adding options to the software.

  17. Mr Angry Says:

    Execute. With an axe. More coders should get that instruction more often! Nice work.

  18. adam Says:

    i love the pseudo-avatar in li.bypostauthor :D

  19. vamsi08 Says:

    nice

  20. Mark Jaquith Says:

    Sort of like on line 20 of trunk’s wp-content/themes/default/archive.php? :-D

  21. Anders Dahnielson Says:

    My first example was a little bit radical, I admit. The following degrades more nicely:

    http://labs.dahnielson.com/files/misc/2006/08/14/sandbox-proposal-2.zip

    (I’m just posting it since I already posted the first one here… ;)

    BTW, keep up the good work and all that!

  22. Scott Says:

    I would suggest that content: ""; is only slightly more reliable than :before or :after.

  23. Andy Says:

    btw I’m using .author-andy, not .bypostauthor :-)

  24. Anders Dahnielson Says:

    Oh, my second proposal was just an conversation item. I completely agree on the fragility. Just throwing out ideas in the open. :-)

    But I think any :before, :after and generated content issues could be fixed with some DOM Scripting for browser that doesn’t suport the pseudo-selectors. I will investigate it, the support and implications.

    TTFN

  25. Dahnielson » Issues in the Sandbox Says:

    [...] I have some beef with the <span class=”metasep”>|</span> because that is somewhat presentational markup, and presentation should be done with CSS. Or so the WASP boys and girls tell me… But as explained by Andy, the introduction of metasep was not an easy decision. [...]

  26. the blogging » One thing leads to another Says:

    [...] After determining the database queries weren’t likely to be too bad with the size of my blog, though the queries and schema could probably use some optimization, I read that wordpress itself is the more likely bottleneck. I happened across Andy Skelton’s post about poorly coded themes, and thought this Sandbox theme sounded interesting. [...]

  27. Andy Skelton: Useless Code in WordPress Themes at SocialCode Says:

    [...] Original post by Andy and software by Elliott Back [...]

  28. Apology: Not Useless Code « Andy Skelton Says:

    [...] Useless Code in WordPress Themes [...]

  29. Nokao Says:

    Nice shot.

    IMHO, every wordpress theme different from SandBox contains useless code.

    I’m converting every website I created in WP+SB.

Leave a Reply