43

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?

2 Answers 2

82

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

5
  • 4
    +1. Always use stylesheet filepath/url to reference the current Theme, and reserve template filepath/url to reference the parent Theme. Commented Apr 5, 2012 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
    Commented Sep 11, 2014 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
    Commented Feb 4, 2015 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
    Commented Aug 7, 2020 at 17:44
  • Ah, Wordpress. Backwards-compatible to a fault. (No sane person would deliberately use such ambiguous naming conventions!)
    – rinogo
    Commented Sep 3, 2021 at 18:38
12

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:

2
  • 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
  • I adjusted the answer accordingly
    – Tom J Nowell
    Commented Aug 25, 2020 at 14:02

Your Answer

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

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