How do i get the base url in template.php?

None of the below works.

global $base_url;
global $base_path;
global $base_root;

5 Answers 5


You can use the base_path() function which should always work.

To be honest though it just returns $GLOBALS['base_path'] so your global $base_path statement should work fine.

  • Yeah but this one worked. May be the $globals are not available in template.php
    – esafwan
    Feb 28, 2012 at 20:08
  • This works nicely to build a PHP header for a View that links to the add content page, eg. <a href="<?php echo base_path(); ?>node/add/offer">Create New Offer</a> Aug 8, 2013 at 16:18

base_path() only produce "/".

to get the full URL including the domain, you have to use: $GLOBALS['base_url']

  • 1
    It works also in Views PHP to to get the site url <?php print $GLOBALS['base_url'] ."/" ; ?> Written in Global PHP - Output code Oct 27, 2017 at 1:49

If you want to get only the base URL, you can use the following line.

global $base_url;

I used it in many projects.


This does not directly answer the question, but seems relevant. If you're looking for the domain name / base url in order to combine it with the aliased path for a node, instead just get the absolute URL like this:

$absoluteurl = url('node/' . $nid, array('absolute' => TRUE));

// http://example.com/content/aliased_path

You can get it from


For more details see http://www.drupal4hu.com/node/310

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.