34

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?

61

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.

Summary

| improve this answer | |
  • 3
    +1. Always use stylesheet filepath/url to reference the current Theme, and reserve template filepath/url to reference the parent Theme. – Chip Bennett Apr 5 '12 at 21:00
  • Unfortunately get_template_directory() returns the entire server path like /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. – NoBugs Sep 11 '14 at 3:41
  • @NoBugs - I was getting the entire server path as well but using get_stylesheet_directory_uri() instead of get_stylesheet_directory() solved the issue. – TheLibzter Feb 4 '15 at 22:19
  • Thank you! most answers i found got me part of the way there with using get_stylesheet_directory() but then I was left in the root directory of my server. get_stylesheet_directory_uri() was the answer to my prayers! 🙏 – sigmapi13 Aug 7 at 17:44
7

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:

| improve this answer | |
  • get_stylesheet_directory() is the correct answer. get_template_directory() will point to the parent theme if a child theme is being used. – Lucas Bustamante Jul 11 at 2:20
  • I adjusted the answer accordingly – Tom J Nowell Aug 25 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.