WordPress at a glance

get_theme_file_path() WP 4.7.0

Gets the path to the specified theme file. Work with child themes.

The function tries to find the specified file in the child theme first. If child theme has no file it gets the path to the file from the main (parent) theme.

This function must be used everywhere while creating a theme in order to any file path of the theme support child theme...

Using this function, you do not need to write special child theme support checks with using get_template_directory_uri() and get_template_directory() functions.

Use get_theme_file_uri() when you need to get the URL of a file rather than it's path.

✈ 1 time = 0.000051s = very fast | 50000 times = 0.55s = very fast | PHP 7.1.2, WP 4.7.3
Hooks from the function
Return

String. The path of the file.

Usage

$filepath = get_theme_file_path( $file );
$file(string)

File name to search for in the theme directory. For example file.php. You can specify the relative file path inc/file.php or so /inc/file.php.

If you leave empty string, function returns the path to the active theme. See. get_stylesheet_directory().

Default: ''

Examples

#1 Attach a theme file and consider that a child theme can be used

We can use this function to allow child theme to overwrite a file of the parent theme.

require_once( get_theme_file_path( 'myfile.php' ) );

As a result, the function will check if there is a file myfile.php in the child theme, if it does not exist, it will be connected from the parent (main) theme.

#2 Include file from inc theme directory

include get_theme_file_path( '/inc/template.php' );

Changelog

Since 4.7.0 Introduced.

Code of get theme file path: wp-includes/link-template.php WP 5.2.2

<?php
function get_theme_file_path( $file = '' ) {
	$file = ltrim( $file, '/' );

	if ( empty( $file ) ) {
		$path = get_stylesheet_directory();
	} elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
		$path = get_stylesheet_directory() . '/' . $file;
	} else {
		$path = get_template_directory() . '/' . $file;
	}

	/**
	 * Filters the path to a file in the theme.
	 *
	 * @since 4.7.0
	 *
	 * @param string $path The file path.
	 * @param string $file The requested file to search for.
	 */
	return apply_filters( 'theme_file_path', $path, $file );
}

Related Functions

From tag: theme path (theme folder url)

More from category: Theme files connection

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register