Wordpress Plugin: ClassyBody

Badly marked-up HTML is the death of CSS creativity. ClassyBody is my WordPress plugin that adds smart CSS classes to the BODY tag.

I love really smart CSS and am always on the lookout for a better way to do things. To that end I just love checking out the latest additions to CSS Zen Garden. This is a site that shows how amazingly you can take exactly the same HTML and create a completely different look and feel, based solely on different CSS. But there is one important matter...

Badly marked up HTML spells the death of CSS creativity. You'll know what I mean if you've ever tried to mess around in MySpace. There is a reason why their HTML is devoid of classes and IDs, and why their CSS is somewhat cobbled... they don't want you to be able to mess with their main page elements!

But CSS Zen Garden's HTML is a good example of how to do it right, as is the website/blog of its creator, Dave Shea - Mezzoblue. Now speaking of that (and getting to the point of this post!), I noticed that Dave's gone with a pretty funky colour scheme approach, where he stays with a particular colour scheme for a few months and then changes to a new one, based on the colours of a picture he chooses for the header. Great idea, and the design is really nice too.

mezzoblue.com

So being a Wordpress user, I wondered how I might achieve something similar, but purely in CSS (Dave passes a querystring parameter to his CSS file, which I assume is dynamically generated.) And this led me to something I've played around with before that's very powerful: adding classes to the Body tag.

If you're going to get smart about CSS, adding classes to your Body tag can add amazing power to what you can achieve in your CSS, since the body tag is the parent element of everything that is displayed on the page. So it follows that any class you add to the body tag is information you have at hand for any CSS styling you apply to any other HTML element on the page. Think about that for a minute.

How can this make Wordpress templates smarter?

Great question, dear Padwan Padawan. For my purposes I'd like to be able to change the appearance of any page element, purely in CSS, based on what kind of webpage my Wordpress blog is displaying. Along the lines of the "is_functions()", I'd like to know when I'm on the homepage, a search results page, a single post, etc. Then I'm free to change the header image, or the page background, or perhaps change the dimensions of the sidebar, or whatever...

But I'd like to go further. I'd like a certain different look to pages related to one of, say, three different categories. Or I'd like all posts from December thru February to have a snowy/winter background theme (for you Northern Hemisphere-ites!).

So to cut a long story short, I created my first (yay!) Wordpress plugin that lets you do all of that, and whatever your imagination would like to do, thanks to the simple addition of classes to the Body tag. (Jumping ahead, I reckon this would make a very logical addition to the Wordpress core, but that's something you can fight for or against in the comments.)

What does this plugin do?

ClassyBody adds contextually-relevant classes to your body tag. That's it! What you do with this added power (and I do mean power) is only limited by your CSS skills. Look out for the next version of my site design, where I am planning to get into this a fair bit...

Since ClassyBody only adds classes to your Body tag, you can safely install this plugin without the fear that it will mess up your existing design, unless - by chance - you're already adding classes to your Body tag. But if not, have no fear firing up this plugin. Its power will only come to the fore when you start messing with your CSS and taking advantage of it.

ClassyBody v1.0 conditionally adds the following classes to your Body tag:

Class
Condition
home
Your blog's homepage.
page
Any static page.
page-[ID]
Any static page (eg. page-4).
post
An individual (single) post.
post-[ID]
An individual (single) post (eg. post-71).
category
A category archive page.
cat-[ID]
A category archive page (eg. cat-12).
archive
A dated archive page.
[Month] [year]
A given month's archives or a single post (eg. March 2006).
search
A search (results) page.
error
A 404 ("page not found") error page.

Why not do this in PHP (like some templates do it)?

Read my lips: PHP for "logic", CSS for presentation. This is why ClassyBody uses minimal PHP processing (just once) to populate the opening Body tag with classes, and then leaves the rest up the CSS. It's all that's needed, assuming you're prepared to put in a little effort with your CSS. No new PHP variables needed, no repeated function calls, no filling up Wordpress template files with more and more PHP. (This is for me all about a cardinal "mantra" I try to stick to: clean HTML with semantic markup, minimal PHP in the template files, as much of the presentation as possible in CSS. Never perfect, but this is the aim.)

Examples

The following examples are very simple and assume the naming convention of the Wordpress Default theme's CSS.

Winter-themed posts. Want all the posts you wrote in Winter to automatically have, say, a snowy-background image? Try something like this (remembering to create and upload the image in question!):

/* a snow theme for winter posts */
body.December, body.January, body.February {
   background-image: url('images/snowy-background.jpg');
   }

Of course, if you're Down Under like me, change the months accordingly.

Different header images. Want to have a special header image for each of the different sections of your blog? Try something like this:

/* the homepage */
body.home #header {
   background-image: url('images/home-header.jpg');
   }

/* static pages */
body.page #header {
   background-image: url('images/page-header.jpg');
   }

/* individual posts */
body.post #header {
   background-image: url('images/post-header.jpg');
   }

/* category archive pages */
body.category #header {
   background-image: url('images/category-header.jpg');
   }

/* dated archive pages */
body.archive #header {
   background-image: url('images/archive-header.jpg');
   }

/* search (results) pages */
body.search #header {
   background-image: url('images/search-header.jpg');
   }

/* 404 error pages */
body.error #header {
   background-image: url('images/error-header.jpg');
   }

Customise a specific category, post or page! You may not want to, but ClassyBody gives you the power to do it. For every category archive listing, and for every individual post, the ID is displayed as a class, in the form cat-[ID], post-[ID] or page-[ID]. This means you can change the appearance of any HTML element on any specific post, page or category listing. Just add the specific customisations to your CSS in the right places.

A live demo - sort of!

I have ClassyBody running on this site, although I have yet to take advantage of it, in CSS. But you can at least see by viewing the source of any page, what classes are being added to the Body. This may be the quickest way for you to see what is added and for it to make the most sense. Make sure to look at an individual (single) post page, as well static pages and a category archive listing.

(Note: If you use this plugin extensively on your blog, let me know and I can add your blog here as an example. Happy to do that for good exampled of usage!)

Is anyone else talking about classes in the Body tag?

Yes, and it's been talked about for a long time. 37 Signals had a post about it, as did A List Apart, and Eric Meyer was talking about something vaguely similar in 2001!. But to the best of my knowledge there certainly is no Wordpress plugin for this, or the promotion of this approach for Wordpress templates in general.

Calling all Wordpress theme designers...

My personal hope is that more and more Wordpress themes will be released and/or retrofitted for this plugin, and that theme designers will take the opportunity afforded by this plugin to get more creative with colours, backgrounds and the CSS customisation of the different kinds of pages in Wordpress. Ultimately I'd hope to see this kind of functionality built into Wordpress core, even though it's a relatively simple bit of PHP and can be hacked into header.php easily enough, even without my plugin (if you so desire).

Installation & Usage

This is a plain vanilla plugin and installs in the normal way:

  1. Download the latest version of ClassyBody.
  2. Change the extension of the downloaded file from .txt to .php.
  3. Upload the renamed file into your /wp-content/plugins/ directory.
  4. Activate the plugin.
  5. Replace the opening Body tag in your active theme's header.php file with:
    <body class="<?php classybody(); ?>">
  6. You're done. Now go fiddle with your CSS!

Version History

  • Version 1.1.20070202 (2 Feb. 2007). Added custom class feature using custom fields for posts and pages. Read this post for details.
  • Version 1.0.20070106 (6 Jan. 2007). Removed extra CR-LFs after closing PHP tag. Think they were causing errors for Jaynee (below) and others. Hope they're fixed now. Sorry folks. Silly error on my part.
  • Version 1.0.20070104 (4 Jan. 2007). Initial public release.

wordpress, plugin, wordpress plugin, body tag, css, stylesheets, wordpress themes, web design, theme design, mezzoblue, csszengarden

Get new posts by email

Essays and notes, sent when they're published. No spam, unsubscribe in one click.