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

New Portfolio Item–Journey Jottings

The new Journey Jottings homepage

I recently completed my first fully Responsive WordPress site for Journey Jottings. Linda contacted me from Australia to ask me if I’d be interested in helping revamp her website.

Linda sells a unique product that combines a map with a journal and allows you to track your travels so that you have a unique record of your journey. She was kind enough to send me samples of some of the maps and magnets that she produces to help inform the direction of the design.

Continue reading

New Portfolio Item — Free Clep Prep

top

I recently had the pleasure of redesigning Free-Clep-Prep.com, an insanely detailed guide to Clep and DSST college examinations in the US. Justin, the owner, had done a stupendous job of putting together one of the most comprehensive online guides I’ve seen on almost any subject and, as a thank you to his many visitors, he hired me to redesign the site.

I thought this showed incredibly good judgment and taste, and liked him immediately.

Continue reading

Optimizing The WordPress Database – Redux

Optimizing Your Wordpress Database

I was poking around the WordPress core code the other day and I discovered a file hidden away in it’s own directory in the wp-admin folder. Further investigation revealed a built in method for optimizing database tables that is a lot easier and safer than the optimizing method I suggested a few weeks back.

Here’s how to use it:

1) Backup your database

I would still recommend that you back up your database before you try to repair it. Follow my instructions for backing up your database from my first post, then come back here to finish the job.

2) Allow Repair Access

Find the wp-config.php file (usually located in the root of your site), open it up and add the following line anywhere – I usually add it at the bottom so I can find it easily when I’m done:

define('WP_ALLOW_REPAIR', true);

3) Optimize your Database Tables

Open up a browser and head over to http://your-site.com/wp-admin/maint/repair.php. You should be presented with the following screen:

If your screen looks like this…

…you haven’t added the code from step 2 to your wp-config.php file correctly – check it isn’t placed next to some comment markers (// or /* ).

Click on the Repair and Optimize Database button. WordPress will shut down your site while it optimizes your database tables for you – it may take a few moments to complete depending on the size of your database.

When it’s finished, you should see a report screen like this telling you which tables have been optimized:

4) Remove Repair Access

Open up your wp-config.php file again, and remove the line you added earlier:

define('WP_ALLOW_REPAIR', true);

This is to stop other people coming along and messing around with the Repair settings without you knowing.

If you had a huge database with hundreds of posts and pages, then doing this may net you a slight increase in the speed of your site. Bonus!

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