0

It's a small thing, but it would be nice if a site running locally could use a different version of a favicon, to make it easier to separate local and live sites visible in browser tabs.

I had in mind a really simple implementation, where a local favicon would just have "_local" appended to the name of the current theme favicon, so:

favicon.ico
favicon_local.ico

Then, if the site is running locally, and if a local version of the favicon exists, use it instead.

My question - what's the best way to implement this? Also, what would be a good way to check if a site is running locally?

Thanks for any ideas, code, or pointers.

1

If you want to try out a code solution, you can mess with the favicon in a hook_html_head_alter().

Something like this would be a starting point, but you likely need to adjust the $_SERVER test:

function MYTHEME_html_head_alter(&$head_elements) { 
  foreach ($head_elements as $key => $value) {
    if (preg_match('/favicon\.ico$/', $key) === 1) {
      if (in_array($_SERVER['HTTP_HOST'], array('localhost', '127.0.0.1')) {
        // adjust $value as needed
        $head_elements[$key] = $value;
      }
    }
  }
}
2

Do you use any versioning like Git? If so, you can just upload the real favicon to your live site, then change the favicon locally and add it to your .gitignore afterwards.

  • I'm not actually using git for this site. But I should be. Thanks for an interesting approach. – Dave Bruns Dec 29 '12 at 4:39
0

Favicon module handles this for the multisite installations. You may get few pointers from the code in the module.

0

Based on MPDs answer above, this is the code I used to define a custom favicon on this site, when it is running locally using mamp pro. This is a function in template.php for the active theme. Seems to work well.

function sitename_html_head_alter(&$head_elements) {

  $path = 'http://sitename/sites/default/files/favicon_local.ico';

  if ($_SERVER['HTTP_HOST'] == 'sitename') {

    foreach ($head_elements as $key => $value) {
      if (preg_match('/favicon\.ico$/', $key) === 1) {
        $head_elements[$key]['#attributes']['href'] = $path;
      }
    }
  }
}
0

You may find the Environment Indicator module is better suited for that purpose.

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.