Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got an issue with changing a header image based on language and the file path to the image. Have tried relative and absolute paths but still end up with a broken/missing file icon.

In my functions I have:

function header_image_switcher() {
    switch( ICL_LANGUAGE_CODE ) {

        case 'en':
            $link_to_image = '/images/top-header-en.png';
            break;

        case 'fi':
            $link_to_image = '/images/top-header-fi.png';
            break;

        case 'sv':
            $link_to_image = '/images/top-header-sv.png';
            break;

        return $link_to_image;
    }
}

and the template contains

echo "<img src=" . header_image_switcher() . " />";

My images folder is outside of the theme and in the same folder as my install but the images still won't load.

Any obvious mistakes I'm making?

share|improve this question
1  
Have you looked at the HTML? Is src empty or just pointing somewhere wrong? –  kraftner May 15 at 13:13
    
Your image folder is inside the WordPress root directory ? So wp-load.php and your images folder are at the same place? –  kaiser May 15 at 13:13
    
Hi...the source comes back empty...just a backslash. Kaiser, yes this the current directory setup. –  JCJ May 19 at 7:42

1 Answer 1

You can use site_url() or get_site_url(). Depending on your install and setup, you could as well use network_site_url() - which is what I would prefer as it falls back to site_url() internally if it is no multisite setup.

The site_url( $path, $scheme ); function also takes two arguments:

  1. The $path can be used to append a path to the URl in case you need to point somewhere custom.
  2. Omit the second one, as it's the scheme (http/s) and will be set automatically - only use it if you want to force a scheme.

Note: You might want to make your switch a bit more fail save:

switch ( $foo )
{
    case 'bar' :
        return 'I am bar';
        break;

    case default :
    case 'baz' :
        return 'I am baz';
        break;
}

Having a default will save you from failing when none of the conditions are met - with language you might want to use American English. Also you can return immediately as there is nothing which shall happen with the switch result afterwards.

share|improve this answer

Your Answer

 
discard

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.