Every now and then I have some period of times when I feel an urge of giving back to the community and keep an eye on the public forums of wordpress.org. The good part is that this synchronizes with the moments when I’m not that busy with my day-to-day job.

This time I found a pretty interesting question related to the Polylang plugin. I usually recommend using the WPML plugin since it’s a pretty great tool (even if it is not easy to be used by a new WordPress user) but this time the question was about a different multi-language plugin. Don’t get me wrong: I like that Polylang is a free plugin that can be used for simple websites.

Let’s get to the point: the question raised by a user on the public forums was about how to have a different logo for each language. I checked a couple articles and topics but I couldn’t find an article to present a solution. I always look for new challenged and that’s why I started working on a custom code to do the job. And here’s the solution that came out.

The code

Here’s the code that need to be added to your website for using different logos when having the English and French languages:

function custom_polylang_multilang_logo( $value ) {
	if ( function_exists( 'pll_current_language' ) ) {
		$logos = array(
			'en' => wp_get_attachment_image('1555', 'full'),
			'fr' => wp_get_attachment_image('1556', 'full'),
		);
		$default_logo = $logos['en'];
		$current_lang = pll_current_language();
		if ( isset( $logos[ $current_lang ] ) )
			$value = $logos[ $current_lang ];
		else
			$value = $default_logo;
	}
	$html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
            esc_url( home_url( '/' ) ),
            $value
        );
	return $html;
}
add_filter( 'get_custom_logo', 'custom_polylang_multilang_logo' );

The logos

Now we have the code, but it cannot know what images to use from the media gallery. Once you uploaded the logos, simply get the ID for each one and replace the 1555 and 1556 in the code above with your values.

How to get the image ID? When opening a logo image in the media library, the link looks something like this:

https://www.example.com/wp-admin/upload.php?item=205

The 205 value is the one that’s needed here. 

Where to add the code?

There are two ways to add the code to the website:

  1. Copy the code that resulted (with the two IDs changed) to the functions.php file of the current theme;
  2. Use a plugin like My Custom Functions. This way, the code will stay there even when changing the theme.

Adding one more logo

If there are more than two languages used, the change is pretty simple. Just simply copy this line of code:

'en' => wp_get_attachment_image('1555', 'full'),

, replace the en part with a new locales (the full list of locales can be found here), and add it right after this line:

'fr' => wp_get_attachment_image('1556', 'full'),

Don’t forget about the logo ID and that’s all. This part of code should look like this:

'en' => wp_get_attachment_image('1555', 'full'),
'fr' => wp_get_attachment_image('1556', 'full'),
'ro' => wp_get_attachment_image('1557', 'full'),

I guess that this solution is needed when a website has some sort of text inside the logo, and it needs to be different in each language. I cannot imagine another situation when someone would have a different logo for different countries 😅

Let me know in the comments area if this did the trick for you or you need a little help with it.

Join the Conversation

23 Comments

  1. when i add the PHP code to the child theme function.php and change it to ‘he’ instead of ‘fr’ and it is not working, maybe i did something wrong…

    this is the new code :
    function custom_polylang_multilang_logo( $value ) {
    if ( function_exists( ‘pll_current_language’ ) ) {
    $logos = array(
    ‘en’ => wp_get_attachment_image(‘211’, ‘full’),
    ‘he’ => wp_get_attachment_image(‘214’, ‘full’),
    );
    $default_logo = $logos[‘he’];
    $current_lang = pll_current_language();
    if ( isset( $logos[ $current_lang ] ) )
    $value = $logos[ $current_lang ];
    else
    $value = $default_logo;
    }
    $html = sprintf( ‘%2$s’,
    esc_url( home_url( ‘/’ ) ),
    $value
    );
    return $html;
    }
    add_filter( ‘get_custom_logo’, ‘custom_polylang_multilang_logo’ );

    i find the image ID and insert it, and still i see the same logo for different languages

    1. Hey!

      Any chance that you could share a link to your website? Maybe I can find the reason why the code is not working. Or if you managed to sort this out in the meantime, an update about it would be great.

    1. Hey Illy,

      Even though you have the image like this: https://www.sharon-nehab.co.il/wp-content/uploads/2019/02/Logo_SN-en.png, you can still go to the Dashboard → Media area and get the ID that I mentioned in the article. Just simply go to this section, look for the Logo_SN-en image, press it, and get the image ID. Here’s a quick video where you can see how to get it: https://www.youtube.com/watch?v=eCBYi14Rkx8

      Can you tell me if you manage to do the change this time?

    2. Thanks for your reply.
      I have added the code with ‘my custom functions’ and it looks like this:
      function custom_polylang_multilang_logo( $value ) {
      if ( function_exists( ‘pll_current_language’ ) ) {
      $logos = array(
      ‘he’ => wp_get_attachment_image(‘4661’, ‘full’),
      ‘en’ => wp_get_attachment_image(‘5429’, ‘full’),
      );
      $default_logo = $logos[‘en’];
      $current_lang = pll_current_language();
      if ( isset( $logos[ $current_lang ] ) )
      $value = $logos[ $current_lang ];
      else
      $value = $default_logo;
      }
      $html = sprintf( ‘%2$s’,
      esc_url( home_url( ‘/’ ) ),
      $value
      );
      return $html;
      }
      add_filter( ‘get_custom_logo’, ‘custom_polylang_multilang_logo’ );

      The thing is my website is Hebrew and English
      1. I want the main lang to be HEBREW
      https://www.sharon-nehab.co.il/he/homepage/
      and I dont know why something went wrong :(\
      2. When you swap to English it goes likr this
      https://www.sharon-nehab.co.il/en/home/
      but the logo doesnt chenage 🙁 to the EN one.
      Pleae can you help?
      Cheers
      illy

      1. Hey Illy,

        I can see that you’re using a theme called Leader. I had a look at it and I could see that the logo option is different. This code is made to work with the logo option from Dashboard → Appearance → Customize → Site Identity → Logo. However, in your case, the logo is set through a theme option. In this case, you can try to translate the logo by following the steps presented here https://polylang.pro/doc/working-with-media/. Also, reaching out the theme authors would be a great option too. They might’ve had this question from other clients and be able to point you to a solution. It’s worth a try.

  2. Thanks for your help, It’s really working!!.but there is a further question to ask, The Logo’s URL disappear and unclickable, What should I do next step? Could you help me? Thanks a lot!

      1. Hello,my website is chundian.com.tw.
        But I’m already fix this issue by other solution, thanks for your reply .

      2. Hi! great solution for the logo change. Thanks!
        I also have the problem with the logo’s url dissapered..
        can you have a look on? thanks..
        tantra-masszazs.hu theme: astra

        1. Hey!

          I made a small change to the main code. The last part (the line that starts with $html) is different now. Just replace that on your website and tell me if the link works fine this time. Have a look at the links on both languages.

          Regards

          1. Hello, Radu
            I noticed you rewrite your code on the last part so I put it on my website and it’s work now!! Thanks for your doing, I deeply appreciate your helping

  3. Hi Radu,
    Thanks for the solution. It works but I only have the logo. I lost the url and its unclickable. What can I do? Thanks!

    1. Hey Nelle!

      The link issue is a mistake I made on the last part of the code. Please check the code again and tell me if things are working fine now.

      Regards

  4. Hi Radu,
    Unfortunately did not work for me, I`m using neve theme, added the code to childthemes function file but still getting only logo in main language..

    1. Hey Sandras,

      Thank you for telling me what’s the theme that you’re using. I just had a look at it and I could see that it’s not using the get_custom_logo function but it’s building it manually instead. The code that I created is made to work over this filter. I think that you’ll have to reach out the theme authors in this case and see if they have any advice about using a different logo on each language.

      Regards

  5. I did everything as you wrote, but there are no changes at all. I have three languages. The code is added to the child theme. And even tried it through the My Custom Functions plugin. But still there is no result. Help me please.

    Here is my code:
    // Смена логотипа при изменения языка
    function custom_polylang_multilang_logo( $value ) {
    if ( function_exists( ‘pll_current_language’ ) ) {
    $logos = array(
    ‘ru’ => wp_get_attachment_image(‘6’, ‘full’),
    ‘en/home/’ => wp_get_attachment_image(‘3516’, ‘full’),
    ‘kk/basty-bet/’ => wp_get_attachment_image(‘3517’, ‘full’),
    );
    $default_logo = $logos[‘ru’];
    $current_lang = pll_current_language();
    if ( isset( $logos[ $current_lang ] ) )
    $value = $logos[ $current_lang ];
    else
    $value = $default_logo;
    }
    $html = sprintf( ‘%2$s‘,
    esc_url( home_url( ‘/’ ) ),
    $value
    );
    return $html;
    }
    add_filter( ‘get_custom_logo’, ‘custom_polylang_multilang_logo’ );

    Website: https://nursultan.interteach.kz/

    1. Hey Salauat,

      I could see that you replaced en with en/home, and kk with kk/basty-bet. Could you add back just en and kk (on the lines with wp_get_attachment_image), and tell me if things work fine this time, please?

      Regards

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: