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

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

Creating a 3D Pipe with Illustrator and Photoshop

Illustrator 3D Objects

We’ll be building the shape in Illustrator using the 3D Revolve tool before going into Photoshop to texture it all up using clipping masks and blend modes and layer styles and all that modern doohickory.

Continue reading

How To Upgrade To WordPress 3 Safely

Header image for the Upgrade to Wordpress 3 post

The recent release of the world’s most popular blogging platform has seen some huge additions, including the ability to add custom Post Types and custom Taxonomies, and support for multiple blogs.

Other improvements include support for custom stylesheets to style the post and page editors. What this means is that what you see in the editor more closely resembles the final product.

To take advantage of these amazing new features, you’ll need to upgrade. Upgrading your current version of WordPress to the new version is pretty easy and all of the core functionality should just continue to work as normal and you shouldn’t lose anything.

However, there have been reports of bugs in some plugins and themes after the new version has been installed, so some care should be taken.

Continue reading