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 might not be that easy for 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. However, I found that there’s no option to simply change the logo added through Appearance → Customize → Site identity.
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.
This solution is gonna work for themes that are using the get_custom_logo
function for getting the logo set in Appearance → Customize → Site identity.
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:
- Copy the code that resulted (with the two IDs changed) to the functions.php file of the current theme;
- 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
'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.
Leave a Reply