How to get the path to your theme in your drupal template

Submitted by charles on
Instead of hard coding your theme path into your drupal theme templates, it's better to use some built in drupal functions or template variables:
  • Use the $directory variable which will be available in your .tpl.php files
  • Use the path_to_theme() function - this provides the same output as the $directory variable
  • Use the base_path() function which will give you the url to the root of your site.
I always use absolute references, for example: An image this includes the domain name and then the theme path. Update: Sometimes in a custom module I need the absolute path (not the url) and I've found using the function realpath() helps, for example, say I want to get the path to the files directory: realpath(".").'/files/' This will output /usr/var/www/whatever/files/.