locate_template()
Retrieve the name of the highest priority template file that exists.
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.
No Hooks.
Return
String
. The template filename if one is located.
Usage
locate_template( $template_names, $load, $load_once, $args );
- $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 - $load_once(true|false)
- Whether to require_once or require. Has no effect if $load is false.
Default: true - $args(array)
- Additional arguments passed to the template.
Default: empty array
Examples
#1 Check for a file before include it
Let's create the possibility to output page content in some special way if necessary. To do this, create a file content-SLAG_PAGE.php
in the theme and use its code instead of the function the_content():
$page_name = $post->post_name; $locate_template = locate_template( "content-$page_name.php" ); if( $locate_template ) { // OK, connect the template file require $locate_template; // or: //get_template_part( 'content', $page_name ); } else { // File not found, load content by default the_content(); }
#2 Getting the file path
echo locate_template( 'header.php' ); //> /home/example.com/public_html/wp-content/themes/pink/header.php
#3 Specify sub-folder
In this function you can specify not only the file name, but also the relative path from the theme root:
locate_template( 'inc/filename.php', true );
This code connect the file filename.php
, which is located in the folder inc
in the directory of the child theme or parent theme if there is no child theme.
#4 User-provided Template path and Directory traversal attacks
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
/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 ); }
Notes
- Global. String. $wp_stylesheet_path Path to current theme's stylesheet directory.
- Global. String. $wp_template_path Path to current theme's template directory.
Changelog
Since 2.7.0 | Introduced. |
Since 5.5.0 | The $args parameter was added. |