How do I get the path of the current drupal theme?
Solution 1
Use the path_to_theme
function.
Solution 2
this should work (doc):
global $theme;
$path = drupal_get_path('theme', $theme);
// there's also a $theme_path global
global $theme_path;
Solution 3
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.
Solution 4
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();
Solution 5
In Drupal 7, for getting current theme's path, we can use: path_to_theme() function.
Comments
-
Steven Noble about 4 years
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?-
golddragon007 over 4 yearsBe 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
-
-
apaderno over 13 yearsIt's better to use
path_to_theme()
, than to use$theme_path
. -
timoxley over 13 yearsWhy is it better to use path_to_theme(), instead of $theme_path?
-
htoip about 12 years@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. -
webdevbyjoss almost 12 yearsnote 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:drupal.org/node/194098
-
Kandinski about 11 yearsI 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.
-
Adaddinsane over 3 yearsThis is not the best way - see other answer.
-
Renrhaf over 2 yearsBest answer for Drupal 8/9 in my opinion