Skip to content

Instantly share code, notes, and snippets.

@scragz
Created January 25, 2011 20:01
Show Gist options
  • Save scragz/795537 to your computer and use it in GitHub Desktop.
Save scragz/795537 to your computer and use it in GitHub Desktop.
<?php
/**
* Retrieve the name of the highest priority template file that exists.
*
* Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
* inherit from a parent theme can just overload one file.
*
* @since 2.7.0
*
* @param array $template_names Array of template files to search for in priority order.
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
* @return string The template filename if one is located.
*/
function locate_template($template_names, $load = false, $require_once = true ) {
if ( !is_array($template_names) )
return '';
$located = '';
foreach ( $template_names as $template_name ) {
if ( !$template_name )
continue;
if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
}
}
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