Returns the path to a system item (module, theme, etc.).

Returns the path to a system item (module, theme, etc.).


seanburlington’s picture

If you need to refer to other files within the same module.


doesn't require a database hit so has better performance

rimian’s picture

Program to an interface not an implementation. That's the whole point of this function. :)

mikeker’s picture

Just to clarify a few points about seanburlington comment on database hits:

  • drupal_get_filename statically caches its results so you only have a DB hit on the first drupal_get_path call for a given module or theme.
  • global $theme_path; is a quick way to get at the path to the current theme without hitting the DB.
  • You can often use relative paths within a module. Use dirname(__FILE__) when you need the whole path.
EvanDonovan’s picture

The path returned by this function will be relative to the DocumentRoot in which Drupal is running.

drupal_pupil’s picture

you may not need to use the path argument of the hook_theme(), instead you can just prefix the value of the template string with the name of the subfolder that contains the template

'template' => 'templates/mymoduletemplate' ,
Aple’s picture

Had some problems with this. Here's a simple way to use this:

    function my_path() {
        return drupal_get_path('theme', 'YourThemeName');
<img src="<?php echo my_path(); ?>/img/test.jpg">

You can condense this down further and include it in a header that's required on every page and end up with a relative path variable like:

<?php echo p(); ?>/img/test.jpg

Hope this helps someone.

teodor.sandu’s picture

It's usually best to avoid calling functions in .tpl files (excepting the obvious use-cases of course).
In this case, why don't you add a variable to all page templates via template_preprocess_page() to hold your relative path?
This would condense all those function calls into just one, or none if you write the p() function contents inside your _preprocess_page() function.

What i mean is writing this in template.php:

function mytheme_preprocess_page(&$vars){
  $vars['p'] = drupal_get_path('theme', 'mytheme');

This avoids all those function calls (since the function above is called only once!), and in your .tpl file you'd have something like this:

<?php echo $p; ?>/img/test.jpg

Note that the function call has been replaced by a variable, thus making it even better, right?


delta62’s picture

Remember that the $name variable is the machine-readable name, which is the name of your .info file, not the human-readable name contained in the .info file. So, for a theme, if your info file is called mytheme.info, and the name variable within that file is My Theme Name, use mytheme for the $name variable.

doublejosh’s picture

Cross linking to function for current theme...


starsinmypockets’s picture

Shouldn't available types be listed or cross-referenced here?

rakesh.gectcr’s picture

like this.
drupal_add_js(drupal_get_path('module', 'project_extras') . '/project_extras.js');
drupal_add_js(drupal_get_path('module', 'yourmodulename') . '/yourmodulename.js');

rakesh.gectcr’s picture

if its in theme
drupal_add_js(drupal_get_path('theme', 'themename') . '/themename.js');

henryhu’s picture

For example, I'd like to get the path of sites/all/libraries/. What can I do?

michaelcchastain’s picture

base_path() . "/sites/all/libraries/";


global $base_path;

$base_path . "sites/all/libraries";

ls206’s picture

Assuming you have the Libraries module installed, you can use libraries_get_path()

Ayesh’s picture

Note that drupal_get_path() does NOT add a trailing slash. So if you want to include some file in module's folder, you will need to add the slash yourself.