2

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
3
  • And what is the problem with get_template_directory()?
    – kero
    Dec 14, 2018 at 12:01
  • @kero ..as coding requirement mentions..It is recommended that the following WordPress 4.7+ functions be used, as they greatly improve the child theming capabilities of the theme:get_theme_file_uri() get_parent_theme_file_uri() get_theme_file_path() get_parent_theme_file_path()
    – user145078
    Dec 14, 2018 at 12:06
  • @kero i have no problem with get_template_directory()..i think it won't allow overriding...not sure if any of other functions as advantage over it.
    – user145078
    Dec 14, 2018 at 12:10

1 Answer 1

0

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.

2
  • That really helps, thank you :)..i think template_directory or stylesheet_directory will be still required for version below 4.7
    – user145078
    Dec 14, 2018 at 14:47
  • 1
    I can’t imagine why anybody writing code now would need to worry about that. That version is already 2 years old. Dec 14, 2018 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.