locate_template( string|array $template_names, bool $load = false, bool $load_once = true, array $args = array() ): string

Retrieves the name of the highest priority template file that exists.

Description

Searches in the stylesheet directory before the template directory and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.

Parameters

$template_namesstring|arrayrequired
Template file(s) to search for, in order.
$loadbooloptional
If true the template file will be loaded if it is found.

Default:false

$load_oncebooloptional
Whether to require_once or require. Has no effect if $load is false.

Default:true

$argsarrayoptional
Additional arguments passed to the template.

Default:array()

Return

string The template filename if one is located.

Source

function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
	$stylesheet_path = get_stylesheet_directory();
	$template_path   = get_template_directory();
	$is_child_theme  = $stylesheet_path !== $template_path;

	$located = '';
	foreach ( (array) $template_names as $template_name ) {
		if ( ! $template_name ) {
			continue;
		}
		if ( file_exists( $stylesheet_path . '/' . $template_name ) ) {
			$located = $stylesheet_path . '/' . $template_name;
			break;
		} elseif ( $is_child_theme && file_exists( $template_path . '/' . $template_name ) ) {
			$located = $template_path . '/' . $template_name;
			break;
		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
			break;
		}
	}

	if ( $load && '' !== $located ) {
		load_template( $located, $load_once, $args );
	}

	return $located;
}

Changelog

VersionDescription
5.5.0The $args parameter was added.
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Note that locate_template() does not prevent directory traversal attacks, so if you’re passing a user-provided template name to the function, be sure to verify that it’s from one of the three appropriate locations (active theme directory, parent theme directory, or /wp-includes/theme-compat/ directory).

    Example:

    $template = locate_template( $template_filename_from_unsanitized_user_input );
    
    // Only allow templates that are in the active theme directory, parent theme
    // directory, or the /wp-includes/theme-compat/ directory (prevent directory 
    // traversal attacks).
    $template_in_theme_or_parent_theme_or_compat = (
    	// Template is in current theme folder.
    	0 === strpos( realpath( $template ), realpath( STYLESHEETPATH ) ) ||
    	// Template is in current or parent theme folder.
    	0 === strpos( realpath( $template ), realpath( TEMPLATEPATH ) ) ||
    	// Template is in theme-compat folder.
    	0 === strpos( realpath( $template ), realpath( ABSPATH . WPINC . '/theme-compat/' ) )
    );
    
    if ( $template_in_theme_or_parent_theme_or_compat ) {
    	require_once( $template );
    }

You must log in before being able to contribute a note or feedback.