Finding Info

The first seven years or so of my career as a software developer were good ones.  One way I felt that I distinguished myself from others wasn’t anything typical like coding up some super efficient algorithm (in fact, I’m quite bad at that), but rather the ability to hunt down information.  Back then Google was just in its infancy.  The primary source of info was the MSDN collection of CDs, books, and magazines, all of which I read and I became very good at searching.  Whenever there was a hard problem facing our team, I just went to these sources and eventually found a solution.

Even with the advent of Google and other search mechanisms, I don’t feel I have the edge that I used to, but here’s a recent example in WordPress of how I hunted down info about solving a problem for a client.  He needed to present admins with a list of templates installed in the user’s theme.  Easy problem, but how do you go about finding the answer?

Thankfully, unlike other open source techs I use now, WordPress is a very clean project where finding answers is pretty easy.  The first idea I had was:  ”Obviously, this must be easy because WordPress itself does this.”  Have a look at the template section of the “page edit” admin page:

This “Page Attributes” area is a box on the right side of the page.  What I usually do in this case is search the WordPress codebase for a visual text string that is likely to turn up only a few times.  In this case, the options weren’t so go, so I just went with the title:  ”Page Attributes:”.  Fortunately, there were results in the file edit-form-advanced.php:

if ( post_type_supports($post_type, 'page-attributes') )
	add_meta_box('pageparentdiv', 'page' == $post_type ? __('Page Attributes') : __('Attributes'), 'page_attributes_meta_box', $post_type, 'side', 'core');

That doesn’t quite get us there, but you can see there’s a function call to “add_meta_box”, so let’s search for “function add_meta_box”.  The results are found in meta-boxes.php:

<p><strong><?php _e('Template') ?></strong></p>
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown($template); ?>
</select>

OK, getting closer.  There’s a function called page_template_dropdown().  Searching the WordPress Codex doesn’t yield much on this function, so let’s search the source again.  This time “function page_template_dropdown”.  Here are the results:

function page_template_dropdown( $default = '' ) {
	$templates = get_page_templates();
	ksort( $templates );
	foreach (array_keys( $templates ) as $template )
		: if ( $default == $templates[$template] )
			$selected = " selected='selected'";
		else
			$selected = '';
	echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
	endforeach;
}

No we’re getting close!  See, there’s a function called “get_page_templates()” which is obviously returning a list of page templates.  Exactly what we want.  At this point, I assumed that there was a documented function in the Codex for this function.  There is, but there’s almost no information on it.  So, it’s time to go to the code one more time.  And of course, I search for “function get_page_templates”.  Here are the results (in theme.php):

/**
 * Get the Page Templates available in this theme
 *
 * @since 1.5.0
 *
 * @return array Key is the template name, value is the filename of the template
 */
function get_page_templates() {
	$themes = get_themes();
	$theme = get_current_theme();
	$templates = $themes[$theme]['Template Files'];
	$page_templates = array();

	if ( is_array( $templates ) ) {
		$base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );

		foreach ( $templates as $template ) {
			$basename = str_replace($base, '', $template);

			// don't allow template files in subdirectories
			if ( false !== strpos($basename, '/') )
				continue;

			if ( 'functions.php' == $basename )
				continue;

			$template_data = implode( '', file( $template ));

			$name = '';
			if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
				$name = _cleanup_header_comment($name[1]);

			if ( !empty( $name ) ) {
				$page_templates[trim( $name )] = $basename;
			}
		}
	}

	return $page_templates;
}

Great!  Exactly what we need.  You can even see the code that implements the behavior as described in the guides on theme development:  That in order to register your template with the system, you just need to add comments at the beginning with the text “Template Name:”  All we need to do now is pop in this function to our code and we’re good to go!

This entry was posted in Software. Bookmark the permalink.

Comments are closed.