Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

e.g. this:

<a href="<?php echo $front_page; ?>" title="<?php echo t('Home');?>" rel="home" id="logo">
        <img src="http://localhost/jamesbondblog/sites/all/themes/jamestheme/images/logo.jpg">

is working.

But this is not:

<a href="<?php echo $front_page; ?>" title="<?php echo t('Home');?>" rel="home" id="logo">
        <img src="<?php echo $base_url; ?>/sites/all/themes/jamestheme/images/logo.jpg">

Can someone explain why?

I am using Drupal 7 and this code is in page.tpl.php

share|improve this question
What is your output for whats not working Q? – Alex Gill Dec 5 '12 at 12:33
I have solved it meanwhile by myself: instead of <?php echo $base_url; ?> I need to use <?php global $base_url; echo $base_url; ?> – Derfder Dec 5 '12 at 12:34
Can I ask why is this global variable not accessible by default? For security reasons? Thanks in advance. – Derfder Dec 5 '12 at 12:35
Wasnt aware this was available to the page template which is probably why you couldnt get it ;-) – Alex Gill Dec 5 '12 at 12:40
up vote 2 down vote accepted

what about global $base_path; ?

share|improve this answer
perfect ;). it's working and without any errors in admin! – Derfder Dec 5 '12 at 13:08

Try this...

In your themes template...

function YOURTHEME_preprocess_page(&$variables) {
   global $base_url;
   $variables['base_url'] = $base_url;

In page template...

share|improve this answer
This way echo $base_url; outputs nothing. You have soething wrong with your code probably. But at least the message in admin of undefined variable dissappeared ;) – Derfder Dec 5 '12 at 12:59

SOLVED (with an error message in admin :(

Instead of <?php echo $base_url; ?>

you need to use <?php global $base_url; echo $base_url; ?>

But it keeps saying in my modules this error message:

Notice: Undefined variable: base_url in include() (line 9 of C:\wamp\www\jamesbondblog\sites\all\themes\jamestheme\page.tpl.php).

Thanks in advance for a fix to get rid of this error message.

share|improve this answer
This isnt the way to go Q... – Alex Gill Dec 5 '12 at 12:44
You can see what variables are available to page template. Go to /includes/theme.inc line: 2516 – Alex Gill Dec 5 '12 at 12:45
I know it's wrong, but it is working in the template. Your way is outputing nothing. – Derfder Dec 5 '12 at 13:00

You can use $GLOBALS['base_url'] instead, for example:

<img src="<?= $GLOBALS['base_url'] ?>/sites/all/themes/foo/images/logo.jpg">
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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