general fun with themes

I know I’ve been quiet — it’s been busy at work and lethargy-inducingly humid once I get home. Plus I’ve been working on a new plugin and setting up a phpbb forum (and working on a skin for that: why is it I can never leave well enough alone?). In any case, I worked out a rather convenient way of being able to drop in a new theme and standardize its sidebar pretty quickly. A new theme has a vanilla sidebar which besides not including all my cool perks, most importantly does not include a theme switcher. Meaning that when I install it, if I didn’t add at least the theme switcher hook, a visitor could wind up trapped in that theme. Ugh. At the same time, dropping in a new sidebar got tedious, especially when there were a few cases I added or changed something and had to migrate those changes across the board.

So I came up with this solution, which is working well for me, though it’s not 100% adaptable to everything (it’s best suited for two column formats with sidebar on left or right, although it seems to be okay when the sidebar is the footer, as the latest trend of single column themes is taking hold). It’s a tinker-under-the-hood process, though, so if you’re not comfortable rooting around in your wordpress files, you may want to pass this up. For the rest of you, if this sounds interesting, roll your sleeves up!

First of all in any theme (or most standard ones), there will be a sidebar.php file. In themes set for WordPress 2.0 and onward, these seem to be wrapped in a div with a sidebar id and using a basic ul for formatting. They’re not completely identical, especially older themes seem to have some kind of menu or other class/id in use. Plus, some themes seem to have an annoying format where the first line (I kid you not) in the sidebar.php file is a </div>!! I haven’t dug into this issue deeply enough to determine whether it’s an unfortunate necessity, or crappy formatting. The upshot of all this, though, meant that I couldn’t use my first solution, which was to move a copy of sidebar.php up to the general themes directory and softlink back to it from within each specific theme directory.

What I did instead was isolate the common elements within the wrappers in the sidebar file and yank those out into the general themes directory in a file I called sidebar-contents.php. I then replaced that part of the code with an include file. So for example, my default theme’s sidebar file looks like this:

<div id="sidebar">
<ul>
 
<?php include (TEMPLATEPATH . '/../sidebar-content.php'); ?>
 
</ul>
</div>

And this has been working a treat. Now when I play around with the format of my sidebar, I change items in the included file, and all changes are instantly available to all the themes.

Told you I was a lazy programmer ;-)

del.icio.us:general fun with themes  digg:general fun with themes

2 Comments »

  1. Brian Layman said,

    June 29, 2006 @ 6:44 am

    This is a VERY good tip. And I think it is a style that should be followed on all themes. On my wife’s blog, and she better not post that picture she took of me wearing her robe yesterday, I have several pages that hang independently off of the blog but carry the whole theme of the blog.

    So the very first thing I do when adopting a new theme (and having 1 theme per blog makes this possible) is determine what must come before and “THE LOOP” and create two php files from that and store them in my own includes directory. What I should do is store them in a directory underneath the theme so that I can theme switch…

    Anyway that give me web pages that look something like this…

     
    [... custom code ...]
     

    I also have a standard include file that contains only my DB login and Password for a particular DB, one that opens the DB connection, and one that closes it. So my pages that query the DB all have a similar structure.

    That makes pages like:

    The Latest Knitting News
    Ships in the Fleet
    Link back to me. Arg!
    Advanced News feeds. Yar!

    really easy to create. (BTW Yes that was a cut and paste of the links, you’re not the only one that’s lazy!) In fact, this was all supposed to be info in one of my first blog posts three months ago but I never got around to it…

  2. Brian Layman said,

    June 29, 2006 @ 6:47 am

    Hmmm, it didn’t like me using your code tag… let’s do this the old fashioned way…
    This is what I was trying to show…

    <?php require(dirname(__FILE__).’/inc/custompage_top.php’); ?>
    [… custom code …]
    <?php require(dirname(__FILE__).’/inc/custompage_btm.php’); ?>

RSS feed for comments on this post · TrackBack URI

Leave a Comment

Bad Behavior has blocked 752 access attempts in the last 7 days.