Loading files for theme and child theme (Require)

There seems to be so many options which one should i go for..it’s really confusing? I have used get_template_directory(). What i want is to use a function that works best with child theme and that should work alone if no child theme is used..pls help

  • get_theme_file_uri()
  • get_parent_theme_file_uri()
  • get_theme_file_path()
  • get_parent_theme_file_path()
  • get_template_directory()
  • TEMPLATEPATH

1 Answer
1

get_theme_file_uri(), get_theme_file_path(), get_parent_theme_file_uri() and get_parent_theme_file_path() are relatively new (introduced in WordPress 4.7) functions that have a couple of significant advantages over using get_template_directory().

  • get_theme_file() and get_theme_file_path () allow you to reference files in your theme in a way that allows them to be replaced by a child theme. This lets you do things like allow images to be replaced, which wasn’t previously possible (without a custom function).
  • They are all filterable, which allows child themes and plugins to replace them with files either outside a theme, or with files that aren’t in the same directory as they were in a parent theme. The filter would also let you prevent the inclusion of a file entirely.

The functions that end in _path() return the path to the files, so would be used for including files for use on the server, while the functions ending in _uri() return the URL to the file, for use on the front-end.

The _parent_theme_ functions let you reference files without letting them be replaced by a child theme.

Since the introduction of these functions the only reason you would use the template_directory or stylesheet_directory functions would be to just get the URL or path to the directory itself.

You shouldn’t need to ever use the TEMPLATEPATH constant.

Leave a Comment