Archives

Archive for October, 2009

Developing a Plugin – TabberList

pluginPart1I’ve been asked by Adam Baker of ManVsDebt fame to create a plugin that will help him organise his Stuff page.

In case you don’t know, Adam Baker, his wife and his young daughter are off travelling the world (currently they’re in New Zealand) and Adam is blogging about his attempts to get out of debt and into life. A noble aim and a pretty damned exciting adventure by all accounts.

Anyhow, one of the pages on his blog features lists of all the stuff that he and his family own. At the moment, it is just a single page with a big long list.

Adam put a call out on Twitter for someone to help re-organise this page and I volunteered my services. So, this weekend at Line In will be mostly about me creating a new plugin which I have tentatively named TabberList.

And, in a fit of productivity, I have also decided that I’m going to blog about my adventures in WordPress Plugin Development Land.

I have tentatively named this “Developing a Plugin”.

Snappy.

Part 1 – Planning

I’ve heard rumours that having some sort of a plan is often a good idea if you’re going to do something big and so, being the kind of man that likes to experience new things, I thought I’d give it a go. Here’s my first attempt:

My Plugin Plan

My Plugin Plan

Not too shabby, right?

I know, I know. I can turn my hand to anything. Did you see the database table outline in the top right hand corner? Pretty proud of that part.

Now that I have a plan firmly in hand, I’m ready to start hacking WordPress to pieces.

Hold on to your mousemats, it’s going to be a hell of a ride*.

*Excitement not guaranteed.

Remember to sign up to my RSS feed so that you don’t miss a step!

Overflow: Hidden

Good evening. Tonight’s topic is floats.

Below we have an oft-encountered situation where two elements are enclosed by a container.

Here is some demonstration XHTML…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head profile="http://gmpg.org/xfn/1">
		<title>Clearing Floats</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel='stylesheet' href='style.css' media='screen' type='text/css' />
	</head>

	<body>
	<div class='wrapper'>

		<div class='container'>
			<div class='floatLeft'>Left Float</div>
			<div class='floatRight'>Right Float</div>
		</div>
		<div class='footer'>
			<p>I should be below!</p>
		</div>
	</div>
	</body>
</html>

…and some CSS.

.wrapper 	{ margin: 0 auto; width: 500px; }

.container 	{ width: 450px; background: #ff0000; padding: 10px;}
.floatLeft 	{ margin: 10px; width: 200px; height: 200px; float: left; background: #00ff00; }
.floatRight { margin: 10px; width: 2px; height: 200px;float: right; background: #0000ff; }
.footer 	{ background: #f0f0f0; }

The first element is floated left and his counterpart is floated right. Underneath there is a footer element such as might be should we wish to continue our document.

Taking a look at the result we see that the containing element has had the audacity to collapse in on itself. Curses!

Left Float
Right Float

Indeed, had there not been 10px of padding on that element, our little red friend would have disappeared from our view completely.

The true nature of the problem manifests itself when we look at the footer content, marked above in grey.

Of course, there are a number of solutions to this problem. One could add a clear like so:

.footer 	{ background: #f0f0f0; clear: both;}

This could be added to the footer element but then a further problem arises if we say, for arguments sake, that we wished to add a top margin to that footer element. It suddenly becomes dependent on the height of the floated elements. Not an ideal situation, as this would require us to use complex mathematics – such as addition – to calculate the size of the top margin based on the height of the floated element.

Extending the clearing logic a little further, you could add another element between the closing container tag and the opening footer element tag:

...
	<div class='wrapper'>

		<div class='container'>
			<div class='floatLeft'>Left Float</div>
			<div class='floatRight'>Right Float</div>
		</div>
        <div style='clear: both;'></div>
		<div class='footer'>
			<p>I should be below!</p>
...

But, young adventurer, this is an ignoble suggestion! It uses an unnecessary element purely for a stylistic effect. We are of finer stock than that, you and I, and require a more elegant solution.

Fortunately, we have one that is so elegant it requires simply two words to be added our containing div styles.

The answer is thus:

...
.container 	{ width: 450px; background: #ff0000; padding: 10px; overflow: hidden;}
...

Magically we see the container div contain the two floating elements as was our intention. No extra markup required!

Left Float
Right Float

A much prettier solution, I’m sure you’ll agree – it reminds one of a fair maiden walking through the park on a summer’s day. Best of all, it works across all browsers, including the dastardly IE6.

Welcome to Line In

Thanks for stopping by!

Over the coming weeks and months there will be regular posts on design and programming related topics, including tutorials on plugin construction and Photoshop design works.

Sign up to my mailing list to get free updates using the form to your right, or subscribe to my RSS feed and get all the latest news direct to your reader.