Wrap Form Elements To Maintain Vertical Rhythm

It’s easy to get obsessive about typography and vertical rhythm, especially when using my plugin that shows you exactly how close to the lines you are.

One of the biggest challenges in maintaining a solid vertical rhythm is form elements. I have tried too many different ways to get them to fit into the flow of text–including countless combinations of padding, margins, borders, line-heights and font-sizes–but unfortunately they simply do not behave themselves consistently cross-browser.

Which is why I had a bit of a double facepalm moment when I realised exactly how easy it could be. Simply wrap them in a containing element and give that element a minimum height:

<div class="form-container">
	<label for="input-element">My Label</label>
	<input type="text" id='input-element'>
</div>

Usually I just go for double the line height:

.form-container {
	min-height: 3em; /* Assuming line-height of 1.5 */
}

/* If you're using LESS/SASS, it's even easier */
.form-container {
	min-height: @lineHeight * 2;
}

Obviously, if you change the font-size of the containing element, this will break your vertical rhythm (you’ll have to re-calculate the min-height based on the new line-height, itself based on the new font-size).

But if you’re using the same font-size, then this is all you need to make sure that everything sits vertically where it should.

If you want more space underneath or above your form container, you could simply use another line-height unit:

.form-container {
	min-height: @lineHeight * 2;
	margin: 0.75em 0;  /* Assume 1.5 line height, divided by 2 */
}

Everything that comes after it will sit on the same line as everything that comes before it.

Getting More Control from the WordPress Settings API

A picture of tabbed settings using the WordPress Settings API

No Sketchbook Sunday this week as I’ve been busy coding some new tools to help speed up my WordPress development.

I’ve been developing a class-based Base Plugin environment that allows me to quickly create new settings pages (on both themes and plugins) and focus on form creating, validation and program logic (I can’t wait until PHP5.3 is more widespread. So many times I could have used Late Static Bindings and proper Namespacing…)

I’ve been using the Settings API but I also wanted to be able to switch between Tabs, Meta boxes and vanilla HTML simply by changing one class variable.

In order for this to work, I needed more control over how the Settings are displayed. The default WordPress function is the do_settings_sections(); but this simply dumps out all of the sections in one go—no good if you’re trying to separate out individual sections on one page in order to use JavaScript-based tabs or individual Meta Boxes.

Digging through the core, I had a look over the Settings API functions. The do_settings_sections(); is actually pretty straightforward. It basically just loops through a huge array of all of the pre-defined section areas. So I adapted it and came up with this single version:

if ( !function_exists('line_in_do_settings_section') ) {
	function line_in_do_settings_section($page, $section) {
		global $wp_settings_sections, $wp_settings_fields;

		if ( !isset($wp_settings_sections) || !isset($wp_settings_sections[$page]) )
			return;

		echo "<h3>{$wp_settings_sections[$page][$section]['title']}</h3>\n";
		call_user_func($wp_settings_sections[$page][$section]['callback'], $wp_settings_sections[$page][$section]);
		if ( !isset($wp_settings_fields) ||
			!isset($wp_settings_fields[$page]) ||
			!isset($wp_settings_fields[$page][$wp_settings_sections[$page][$section]['id']]) )
		return;
		echo '<table class="form-table">';
		do_settings_fields($page, $wp_settings_sections[$page][$section]['id']);
		echo '</table>';
	}
}

Now all I need to do is pass the $page and $section that I want to display, and it’ll only display that section. With this level of control, it’s trivial to set up a looping system (for example with tabs):

<?php
// A bunch of pre-defined setting section ids
$tabs = array(
	'default-settings',
	'advanced-settings'
);

<div class='wrap line-in-plugin'>
	<?php screen_icon( 'plugin' ); ?>
	<h2 class='lif-title'>
		<span><?php _e( 'My Example Plugin', $this->lang ); ?></span>
		<?php
		$i = 1;
		foreach ( $tabs as $tab ) { ?>
			<a class="nav-tab hidden" href='#tab-<?php echo $i; ?>'><?php _e( 'Tab ' . $i, $this->lang ); ?></a>
		<?php
		$i++;
		} ?>
	</h2>
	<form action="options.php" method="post">
	<?php $this->settings->get_settings_fields('my-settings-page'); ?>

		<?php
		$i = 1;
		foreach ( $tabs as $tab ) { ?>
			<div id='tab-<?php echo $i; ?>' class='lif-tab'>
				<?php line_in_do_settings_section('my-settings-page', $tab); ?>
			</div>
		<?php
		$i++;
		} ?>	

	 <p class='submit'>
	 	<?php submit_button(); ?>
	 </p>

	</form>
</div>

Instant tabbed settings pages!

A picture of tabbed settings using the WordPress Settings API

Installing PEAR 1.9.2 with MAMP and PHP 5.3 on Mac OSX 10.6

Installing Pear 1.9.2 with MAMP

Installing PEAR 1.9.2 with MAMP 1.9.1 and PHP 5.3.2 on Mac OSX 10.6

This was a lot harder than I think it should have been. I tried following the instructions on the following sites but despite saying that there was “nothing to upgrade” it was still saying version 1.9.0.

http://serverfault.com/questions/184741/upgrading-pear-from-1-9-0-to-1-9-1-fails
http://startrekguide.com/community/viewtopic.php?f=50&t=14728
http://jamsyoung.com/2011/02/11/upgrading-pear-on-mamp/

Using the “go-pear.php” method resulted in the following error:

"Sorry! Your PHP version is too new (5.3.2) for this go-pear."

and using the “pear upgrade pear” method resulted in

"Download of "pear/Console_Getopt" succeeded, but it is not a valid package archive".

At one point I also got the amusing error message saying that it couldn’t upgrade to 1.9.1 because pear/Console_Getopt needed version 1.9.1 to install.

Finally (after installing it in the wrong place twice) I found this method which did work:

Installing Pear 1.9.2

1) Open a new terminal session.

2) Edit your .bash_profile:


edit ~/.bash_profile

so that it contains the following (add it under anything you already have).


PATH="/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php5.3/bin:$PATH"

3) Close terminal and re-open it then type

echo $PATH

It should contain the new MAMP paths. If it doesn’t, something went wrong (make sure you’re in your home directory).

4) Check that you’re running the right PHP (i.e. the one included with MAMP, not the one included in OSX) by typing:

which php

It should read:

/Applications/MAMP/bin/php5.3/bin/php

5) If it says

/usr/bin/php

then you need to either create a symlink to the MAMP version or delete it from here and update your path in your .bash_profile (see above — remember to restart terminal to start a new session).

I backed up the original Snow Leopard versions of PHP and Pear and then deleted them by doing the following:

cd /usr/bin
sudo cp php php.bak
sudo cp pear pear.bak
sudo rm php
sudo rm pear

6) Download pear.php.net/go-pear.phar to /Applications/MAMP/bin/php5.3/:

cd /Applications/MAMP/bin/php5.3
curl -O http://pear.php.net/go-pear.phar
php go-pear.phar

7) You’ll be asked to set the directories. You only need to set the first one and the rest will be correctly created. Choose “1″ and then type:

/Applications/MAMP/bin/php5.3

Hit return.

8) It will then ask you if you want to update your php.ini. Type “Y” and hit enter.

Once it’s all installed, you’ll see a message saying that you have to update your $PATH variable. If you haven’t already (see step 1), do this now then restart Terminal.

9) Type pear -V and it should now be 1.9.2.

You can then install all the packages you like. Remember that PHPUnit is now on its own channel and you’ll have to make pear aware of that new channel by running

pear channel-discover pear.phpunit.de

Create a Quick Grassy Hill in Illustrator with the Crystallize Tool

Quick Grassy Hill in Illustrator

Illustrator offers some very powerful tools for doing damage to your designs. In this tutorial, I want to show you how to quickly create a grassy hill using the Crystallize tool.

1) Start with a new document (Cmd/Ctrl + N), and give it a name. In a fit of originality, I called mine ‘Quick Grassy Hill’. Set the dimensions to 600px wide and 400px high.

Set Up Illustrator

Continue reading

Optimize Your WordPress Database

Optimizing Your Wordpress Database

WordPress is an amazing publishing platform. Part of its greatness is how user friendly it is, with Revisions being a particular case in point. I can’t count the number of times that I’ve had to load up a previous version of a post to reclaim some literary nugget that I accidentally deleted in a fit of writer’s angst.

Unfortunately, in order to save me from myself, it has to use quite a lot of database space. Each revision means one more row in the Posts table, on top of the canonical version that you create every time you hit the Update or Draft versions. If you are like me and painfully obsessive about every single word, or you write epic 5,000 word posts, then that one post could have 20 or 30 revisions saved in addition to your working copy.

Multiply that by the number of posts on your blog, and it could quickly run up to the hundreds or thousands of rows.

Continue reading