get_theme_file_path

Retrieves the path of a file in the theme.

Description

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

Searches in the stylesheet directory before the template directory so themes which inherit from a parent theme can just override one file.

Returns (string)

The path of the file.

Parameters (1)

0. $file — Optional. (string) => ''
File to search for in the stylesheet directory.

Usage

  1. if ( !function_exists( 'get_theme_file_path' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. File to search for in the stylesheet directory. 
  5. $file = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_theme_file_path($file); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_theme_file_path( $file = '' ) { 
  2. $file = ltrim( $file, '/' ); 
  3.  
  4. if ( empty( $file ) ) { 
  5. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { 
  6. $path = get_stylesheet_directory() . '/' . $file; 
  7. } else { 
  8. $path = get_template_directory() . '/' . $file; 
  9.  
  10. /** 
  11. * Filters the path to a file in the theme. 
  12. * @since 4.7.0 
  13. * @param string $path The file path. 
  14. * @param string $file The requested file to search for. 
  15. */ 
  16. return apply_filters( 'theme_file_path', $path, $file );