3

How do I get the domain name so that I can put an absolute path for images in my theme? I'm developing on a development server and want to move it live eventually.

I can't figure out a way to get the domain.

  • If this helped you than please accept it, or if you have found other way to do this than please share that, as it will help others looking for something like this. – Ankit Agrawal Oct 28 '13 at 11:22
6

While $GLOBALS['base_url'] will give you the domain, it really isn't a good idea to use it directly. URLs should either use url() or file_create_url(). For example,

$path = drupal_get_path('theme', 'MYTHME');

drupal_add_html_head(array(
  '#tag' => 'link',
  '#attributes' => array(
    'href' => file_create_url($path . '/images/touch-icon-iphone.png'),
    'rel' => 'apple-touch-icon',
  ),
), 'MYTHME-touch-icon-iphone');

will add the iPhone icon meta to the <head> from the images/ directory from a theme. Leveraging the API will allow paths to be altered, which is necessary for module like CDN to do their thing.

  • Hah, can't believe I never knew file_create_url() could be used for this sort of thing - I thought it was for stream wrapped URIs only! – Clive Oct 14 '13 at 15:28
3

You can use the global variable base_url to get the value of domain, it returns the base URL of the Drupal installation.

global $base_url;

However if you want to get the path of theme you may use the drupal_get_path() as below:

drupal_get_path('theme', 'THEME_NAME');

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.