7

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

None of the below works.

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

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 '12 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> – Joe Beuckman Aug 8 '13 at 16:18
8

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 – Waluyo Adi Siswanto Oct 27 '17 at 1:49
2

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

global $base_url;

I used it in many projects.

1

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
0

You can get it from

$GLOBALS['base_root']

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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