Skip to content

Instantly share code, notes, and snippets.

@cheh
Forked from scribu/wrapping.php
Created March 5, 2014 09:33
Show Gist options
  • Save cheh/9364098 to your computer and use it in GitHub Desktop.
Save cheh/9364098 to your computer and use it in GitHub Desktop.
WP: Theme Wrapping method
<?php
# License: Public Domain
# I recommend replacing 'my_' with your own prefix.
function my_template_path() {
return My_Wrapping::$main_template;
}
function my_template_base() {
return My_Wrapping::$base;
}
class My_Wrapping {
/**
* Stores the full path to the main template file
*/
static $main_template;
/**
* Stores the base name of the template file; e.g. 'page' for 'page.php' etc.
*/
static $base;
static function wrap( $template ) {
self::$main_template = $template;
self::$base = substr( basename( self::$main_template ), 0, -4 );
if ( 'index' == self::$base )
self::$base = false;
$templates = array( 'wrapper.php' );
if ( self::$base )
array_unshift( $templates, sprintf( 'wrapper-%s.php', self::$base ) );
return locate_template( $templates );
}
}
add_filter( 'template_include', array( 'My_Wrapping', 'wrap' ), 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment