I have in the following code in the template:

<img src="<?php header_image(); ?>" />

However, I want to change this image according to the language selected (through GET parameter lang).

The question is, how to change this function to look for different images according to the received GET parameter?

I thought of:

  1. Change this code on header.php to a fixed image path, so I could put an if and choose between the images I want.
  2. Replace the result of the function get_header_image() and use it instead on header.php according to the received parameter, sort of replacing the string suffix .png to _en.png or .png to _es.png.
  3. Change the header_image() function to behave the way I want.

All these ways look ugly to me. Are there better ways?


Filter the get_theme_mod() function (wp-includes/theme.php):

add_filter( 'theme_mod_header_image', 'localized_header' );

function localized_header( $img )
    // Do something awesome with the $img path.
    return $img;
| improve this answer | |

I used second approach (I'm posting what I did as it may be helpful to someone):

    <img src="<?= 
       isset($_REQUEST['lang']) ? 
          (str_replace('.jpg', '_' . $_REQUEST['lang'] . '.jpg', get_header_image())) 
          : get_header_image() 
     ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
| improve this answer | |
  • Never use request arguments without escaping! Use esc_attr() instead. – fuxia Jun 6 '11 at 18:50

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.