WordPress at a glance

locate_template() WP 1.0

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

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

Works based on: load_template()
✈ 1 time = 0.000039s = very fast | 50000 times = 0.16s = very fast PHP 7.1.2, WP 4.7.3

No Hooks.

Return

String. The template filename if one is located.

Usage

locate_template( $template_names, $load, $require_once );
$template_names(string/array) (required)
Template file(s) to search for, in order.
$load(true/false)
If true the template file will be loaded if it is found.
Default: false
$require_once(true/false)
Whether to require_once or require. Has no effect if $load is false.
Default: true

Code of locate_template: wp-includes/template.php VER 4.9.8

<?php
function locate_template($template_names, $load = false, $require_once = true ) {
	$located = '';
	foreach ( (array) $template_names as $template_name ) {
		if ( !$template_name )
			continue;
		if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
			$located = STYLESHEETPATH . '/' . $template_name;
			break;
		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
			$located = TEMPLATEPATH . '/' . $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, $require_once );

	return $located;
}

Related Functions

From category: Theme files connection

No comments
    Hello, !     Log In . Register