1

I am working on a website for a friend of mine, which is multilingual. Therefore I use the plugin Polylang. 'The theme I use is called 'spacious'.

I currently face the problem, that I cant add different logos for different languages, aswell as different header texts/background images.

I googled a bit, and found a possible solution, but I didnt get it to work.

function pojo_polylang_get_multilang_logo( $value ) {
if ( function_exists( 'pll_current_language' ) ) {
    $logos = array(
        'en' => 'logo_en.jpg',
        'fr' => 'logo_fr.jpg',
        'de' => 'logo_de.jpg',
        'es' => 'logo_esp.jpg',
    );
    $default_logo = $logos['en'];
    $current_lang = pll_current_language();
    $assets_url = get_stylesheet_directory_uri() . '/images/';
    if ( isset( $logos[ $current_lang ] ) )
        $value = $assets_url . $logos[ $current_lang ];
    else
        $value = $assets_url . $default_logo;
}
return $value;
}
add_filter( 'theme_mod_image_logo', 'pojo_polylang_get_multilang_logo' );

In my understanding, this function first stores the preset Logo filenames in an array, then checks for the current language and depending on the current language, the add_filter() function sets the current logo.

I tried copying this into the functions.php file but it didnt work. My thought is, that it doesnt work, because of the first parameter 'theme_mod_image_logo'. I also googled this php function to get a better understanding of it and to know what parameter to pass to the function. Can someone explain to me who is firm with php, what parameter I need to pass or if you have a better solution for me?

In case this works for the logo, I could also apply this function to a header-image right? Just change the filenames in the array and pass the right parameter?

Thanks already, I would really appreciate your help!! I need to leave now for about one hour or two, and wont be able to respond back during that time!

Edit: I also had a lokk at 'duplicate' question. It didnt help me since, my understanding is not good enough

Constantin M
  • 119
  • 1
  • 4
  • 18

1 Answers1

2

I think the best approach is to hook into "get_custom_logo", which is used by your theme:

add_filter( 'get_custom_logo', 'my_polylang_logo' );
function my_polylang_logo() {
   if ( function_exists( 'pll_current_language' ) ) {
      $logos = array(
         'en' => 'logo_en.jpg',
         'fr' => 'logo_fr.jpg',
         'de' => 'logo_de.jpg',
         'es' => 'logo_esp.jpg'
      );
      $current_lang = pll_current_language();
      $img_path = get_stylesheet_directory_uri() . '/images/';
      if ( isset( $logos[ $current_lang ] ) ) {
         $logo_url = $img_path . $logos[$current_lang];
      } else {
         $logo_url = $img_path . $logos['en'];
      }
      $home_url = home_url();
      $html = sprintf( '<a href="%1$s" rel="home" itemprop="url"><img src="%2$s"></a>', esc_url( $home_url ), $logo_url);
   }
   return $html;   
}

(untested)

joko13
  • 638
  • 5
  • 16
  • This worked perfectly, a thousand thanks!!! It displays the correct logos in my language, Only thing I now have to is set the right width and height!:-) One more question. Can I use parts of the code and add it to header.php to change the background image of a div? I think I will able to that! – Constantin M Nov 06 '18 at 18:12
  • You can control width and height via CSS or add it directly to the img tag in my code. – joko13 Nov 06 '18 at 18:27
  • Yes thanks, I did it directly in the css editor within wordpress! Works like a charm!! :-) – Constantin M Nov 06 '18 at 19:10