26

The Drupal API has drupal_get_path($type, $name) which will give the path of any particular theme or module. What if I want the path of the current theme?

kiamlaluno
  • 24,790
  • 16
  • 70
  • 85
Steven Noble
  • 9,304
  • 12
  • 42
  • 56
  • Be careful using the current theme path. If somebody creates a subtheme from your theme, the current theme path will be the subtheme's path, not yours! You may break your own theme or the subtheme. - D8 at least – golddragon007 Sep 03 '19 at 12:29

8 Answers8

27

Use the path_to_theme function.

Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
FGM
  • 2,690
  • 1
  • 27
  • 30
  • 3
    note that when called from inside "theme_*" function path_to_there will return path to current module instead of path to currently active theme which can lead to errors, see more details in this bug report:http://drupal.org/node/194098 – webdevbyjoss Jun 20 '12 at 12:29
20

this should work (doc):

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

// there's also a $theme_path global

global $theme_path;
Owen
  • 76,727
  • 20
  • 113
  • 113
  • It's better to use `path_to_theme()`, than to use `$theme_path`. – kiamlaluno Sep 29 '10 at 20:53
  • 2
    Why is it better to use path_to_theme(), instead of $theme_path? – timoxley Feb 01 '11 at 05:12
  • @timoxley, the only difference is that if the `$theme_path` is not set the theme gets initialized and then the new `$theme_path` is returned. See [path_to_theme](http://api.drupal.org/api/drupal/includes!theme.inc/function/path_to_theme/6). – htoip Apr 25 '12 at 11:44
  • I noticed path_to_theme() and $theme_path used inside a field template is giving me the path to module field/module. The only that displayed correctly was $theme. – Kandinski Mar 14 '13 at 10:57
10

In D6 path_to_theme() may not behave in a way you expect depending on how you are using it. If you are using it outside any theme preprocess functions, then it will probably give you what you want, but if it is being called within the context of a module's theming/preprocess hook function... it will be pointing to the module path that declared the theme.

Ex. If i have a theme "my_theme" and my module "my_module" which is overriding the forum themes using the preprocess hooks, calling path_to_theme() within my module: e.g. my_module_preprocess_forums()... will return "forums", and not "my_theme" as one might expect.

Very fruity if you ask me.

5

In Drupal 7, for getting current theme's path, we can use: path_to_theme() function.

Pupil
  • 23,141
  • 5
  • 40
  • 62
4

In Drupal 8

global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';
2

In Drupal 8, if you need to get the active theme path when you have the admin theme active you can fetch the default theme path:

$themeHandler = \Drupal::service('theme_handler');
$themePath = $themeHandler->getTheme($themeHandler->getDefault())->getPath();
JamesWilson
  • 2,027
  • 23
  • 32
Arie
  • 21
  • 5
1

In Drupal 5, you can simply use: path_to_theme()

This will give you a complete path from the root of Drupal to the specific theme directory. Be aware, it does not include a trailing slash.

In Drupal 6, this behaves just a bit differently. If you call it from within your pages, it will call whatever is currently doing the theming... whether that is your theme, a module, etc. Here's the key quote from the API docs:

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.

Source: http://api.drupal.org/api/function/path_to_theme

CaseySoftware
  • 3,003
  • 18
  • 18
0

For D8, the theme folder is available in preprocess functions:

function hook_preprocess_page(&$variables) {
  $variables['some_logo_file'] = "/{$variables['theme']['path']}/images/logo.png";
}

page.html.twig:

<img src="{{ logo_src }}">
s6712
  • 466
  • 4
  • 14