admin管理员组

文章数量:1130349

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

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
Share Improve this question asked Dec 14, 2018 at 11:58 user145078user145078 3
  • And what is the problem with get_template_directory()? – kero Commented 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 Commented 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 Commented Dec 14, 2018 at 12:10
Add a comment  | 

1 Answer 1

Reset to default 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.

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

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
Share Improve this question asked Dec 14, 2018 at 11:58 user145078user145078 3
  • And what is the problem with get_template_directory()? – kero Commented 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 Commented 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 Commented Dec 14, 2018 at 12:10
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: Loading files for theme and child theme (Require)