Skip to content

Instantly share code, notes, and snippets.

@jmdodd
Created February 9, 2012 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmdodd/1782766 to your computer and use it in GitHub Desktop.
Save jmdodd/1782766 to your computer and use it in GitHub Desktop.
Locate a template from a BuddyPress plugin template call.
<?php
// Example usage: ucc_bp_locate_template( 'template/index.php', true, true, __FILE__ ).
if ( ! function_exists( 'ucc_bp_locate_template' ) ) {
function ucc_bp_locate_template( $template_names, $load = false, $require_once = true, $file = '' ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name )
continue;
// Child theme.
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
// Parent theme.
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
// Last-ditch attempts at location of calling file.
} elseif ( $file != '' ) {
$pathinfo = pathinfo( $file, PATHINFO_DIRNAME );
$plugin = plugin_basename( $pathinfo );
// Don't go past the calling plugin's basedir.
$levels = count( explode( '/', $plugin ) ) - 1;
for ( $level = 0; $level < $levels; $level++ ) {
if ( file_exists( $pathinfo . '/' . $template_name ) ) {
$located = $pathinfo . '/' . $template_name;
break;
}
$pathinfo = dirname( $pathinfo );
}
}
}
if ( $load && '' != $located )
load_template( $located, $require_once );
return $located;
} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment