Organizing Themes with Subdirectories

Now that Andrew Nacin has announced that WordPress 3.4 will allow page templates to reside in subdirectories, I am finally going to be able to organize my theme directories with headers, footers, sidebars and page templates all sitting in their own respective directory.

This is just a personal preference for keeping my themes organized, so that if a theme involves a lot of different templates they aren’t just bunched together in the main theme folder. The methods for get_header, get_footer, get_sidebar and get_template_part all reside in ‘includes/general-template.php’.

Here is the relevant code from general-template for ‘get_header’:

function get_header( $name = null ) {
	do_action( 'get_header', $name );

	$templates = array();
	if ( isset($name) )
		$templates[] = "header-{$name}.php";

	$templates[] = 'header.php';

	// Backward compat code will be removed in a future release
	if ('' == locate_template($templates, true))
		load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}

In my themes I use a modified version of these functions to put the header, footer and sidebar in their own folders:

function bfp_header( $name = null ) {
	do_action( 'bfp_header', $name );

	$templates = array();
	if ( isset($name) )
		$templates[] = "headers/header-{$name}.php";

	$templates[] = "headers/header.php";

	// Backward compat code will be removed in a future release
	if ('' == locate_template($templates, true))
		load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}
add_action('get_header', 'bfp_header');

By renaming the functions there is no conflict with the original. To call it in the theme you just call it by the new template tag: bfp_header(), bfp_footer(), bfp_sidebar().

Share on TwitterShare on LinkedInShare on Tumblr

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>