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?

Published by

Andy Skelton

Code Wrangler @ Automattic youtube.com/AndySkelton

29 thoughts on “Useless Code in WordPress Themes”

  1. 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.

  2. 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?

  3. 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.

  4. 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’.

  5. 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?

  6. 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!🙂

  7. 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

  8. 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

  9. Nice shot.

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

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

Comments are closed.