This code is used to get the directory of the current plugin: plugin_dir_url( __FILE__ )
.
What should I use to get the directory of the current theme?
I think you have to be a little careful because it depends on what you are trying to do.
If you are using a child theme get_template_directory();
will still go to the parent theme. However get_stylesheet_directory();
will go to the current theme, child or parent. Also, both these functions return absolute server paths.
If you wanted a fully formed URI, for links or images, you should use get_template_directory_uri();
or get_stylesheet_directory_uri();
using the correct one for the reasons stated.
get_stylesheet_directory()
: file path to current Theme directoryget_stylesheet_directory_uri()
: url path to current Theme directoryget_template_directory()
: file path to parent Theme directoryget_template_directory_uri()
: url path to parent Theme directorystylesheet
filepath/url to reference the current Theme, and reserve template
filepath/url to reference the parent Theme.
Commented
Apr 5, 2012 at 21:00
/var/www/the/path/of/actual/wp-content/themes/mytheme
which is not what you want for doing stuff with $wp_filesystem if WP is connecting through FTP.
get_stylesheet_directory_uri
is what you want. it returns the URL of the current theme. get_stylesheet_directory
will return the file path on the server.
For example:
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/image.png" />
If you need the parent theme, get_template_directory
and get_template_directory_uri
are the equivalents.
If there is no parent theme then both will return the same value.
Further reading:
get_stylesheet_directory
/var/www/yoursite/wp-content/themes/child_theme
get_template_directory
/var/www/yoursite/wp-content/themes/parent_theme
get_stylesheet_directory_uri
https://example.com/wp-content/themes/child_theme
get_template_directory_uri
https://example.com/wp-content/themes/parent_theme
get_stylesheet_directory()
is the correct answer. get_template_directory()
will point to the parent theme if a child theme is being used.
Commented
Jul 11, 2020 at 2:20