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';

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

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

        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?

  • 1
    Have you looked at the HTML? Is src empty or just pointing somewhere wrong? – kraftner May 15 '14 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 '14 at 13:13
  • Hi...the source comes back empty...just a backslash. Kaiser, yes this the current directory setup. – JCJ May 19 '14 at 7:42

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';

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

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.

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.