Drupal - How to get the URL of a file located in the theme folder?

You can use drupal_get_path() to get the path to your theme folder:

$path = drupal_get_path('theme', 'name_of_theme');

From there you can specify a particular file like this:

$image_path = $path . '/images/image.jpg';

You could also use path_to_theme(), but be wary as the return from this can change depending on the context it's called in:

It can point to the active theme or the module handling a themed implementation. For example, when invoked within the scope of a theming call it will depend on where the theming function is handled. If implemented from a module, it will point to the module. If implemented from the active theme, it will point to the active theme. When called outside the scope of a theming call, it will always point to the active theme.


$imgurl = file_create_url(path_to_theme().'/images/blah.png');

description: this line of code uses path_to_theme(), to get the path to the theme, and then file_create_url(), to create an url for the file, to get the URL of an image located in the theme folder, as requested by the op.

I got a downvote for not describing it, so there you are :-)

its also the same as the accepted answer, just with comments applied to make it more complete. please note that it refers to the theme folder of the current theme, so its mainly usefull inside a theme template.

Tags:

Uri

Theming

7