7

I have a multilingual website. Is there a way I can change the logo.png to a different .png after I switch to "India"? I am using polylang plugin at this moment. I tried this solution but it did not work - https://support.pojo.me/docs/polylang-change-logo-every-language/.

Does any one know how to fix this issue?

my code

function pojo_polylang_get_multilang_logo( $value ) {
    if ( function_exists( 'pll_current_language' ) ) {
        $logos = array(
            'en' => 'logo-en.png',
            'in' => 'logo-in.png',
        );
        $default_logo = $logos['en'];
        $current_lang = pll_current_language();
        $assets_url = get_stylesheet_directory_uri() . '/assets/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' );
James Jones
  • 3,850
  • 5
  • 25
  • 44
core114
  • 5,155
  • 16
  • 92
  • 189
  • I recommend taking the code from the link you've tried and editing the question to put it in. You are far more likely to get help if you show people what you've tried. At face value, the code in that article looks fine so you should also paste any errors you're getting. More detail would be good too. As it stands you have barely any detail. Are you actually using the multi-language Polylang plugin mentioned in that link? The code in that link wouldn't work without that plugin active. Stupid question maybe, but how would I know? – James Jones Mar 19 '18 at 06:34
  • Sit I Updated my question – core114 Mar 19 '18 at 06:44
  • Are You getting any errors?? – Darsh khakhkhar Mar 19 '18 at 06:57
  • @Darshkhakhkhar Sir no any errors,image not displayed when I switched the language – core114 Mar 19 '18 at 08:19
  • @core114 what value are you getting in $current_lang ? – Darsh khakhkhar Mar 19 '18 at 09:10
  • @Darshkhakhkhar Sir, English . short code `en` – core114 Mar 19 '18 at 09:40
  • @core114 is $current_lang value is changing when you change the language?? because your default logo is set to $logos['en'] and current_lang value is also en so logo won't change if $current_lang value is not changing. – Darsh khakhkhar Mar 19 '18 at 09:49
  • Sir,Im not clear Can you put the answer using my code – core114 Mar 19 '18 at 09:53
  • What theme are you using? Do you have the logos uploaded to the correct path in you child theme folder? – Ali_k Mar 19 '18 at 11:00
  • Forgot to ask if you have a child theme. Also, it would be great if you can copy the full code responsible for displaying your logo. – Ali_k Mar 19 '18 at 11:05
  • @Ali_k Sir Im used that theme https://themeforest.net/item/republik-government-wordpress-theme/20567269 – core114 Mar 20 '18 at 03:41
  • I can't find documentation about `theme_mod_image_logo`. Isn't that specific to a specific theme? Do you use that theme? – PaulH Mar 23 '18 at 22:09
  • Can you `var_dump(pll_current_language());` or `write_log(pll_current_language());` to make sure that the Indian language abbreviation is **in** . Since if it is returning a different language code always the english language will be used. – Omar Tanti Mar 24 '18 at 14:46

6 Answers6

2

We have done almost the same on a blog that contains 14 categories, and each category had to display its own logo.

In our case, we used custom php code, that checks in the url and overrides the logo display in the theme accordingly, while fetching the logo from the logo path from the db.

In your case, it should be easier since the language variable is easily accessible, so all you need to do in your theme's header.php is a if statement around the logo while fetching the logo image from the database (preferably from the options table) depending on the language.

//fetch the current language ( you can use https://polylang.pro/doc/function-reference/)

$current_language = pll_current_language();

//while fetching the logo for the current language, it's best if you add a fallback to the default language in case the option for the current language is not set

$logo_image = get_option("logo_".$current_language,"logo_".$pll_default_language());

?> <img class="logo" src="<?php echo $logo_image; ?>"

Hope this is what you're looking for.

nitrex
  • 522
  • 6
  • 16
1

in your code you have set the $default_logo = $logos['en']; and your $current_lang value is also 'en' so when you change the language your $current_lang value need to change as per the selected language's short code otherwise your $default_logo and $value will be the same..

Darsh khakhkhar
  • 666
  • 5
  • 15
1

You can easily register a string called LOGO and put all links (or names) in it!

Step 1: Add below code in functions.php

pll_register_string("LOGO","example.com/path/to/images/logo.png"); // this url for default language

Step 2: Now in your wp-admin in "string translations" under Languages, you have "LOGO" field available in all languages.

preview

Step 3: To get logo URL (in all languages) just use this code:

<?php pll_e("LOGO"); ?>

Note: if you want put only logo name in this field, your code should be like below:

<?php 
$assets_url = get_stylesheet_directory_uri() . '/assets/images/';
echo $assets_url; pll_e("LOGO"); ?>
aidinMC
  • 1,415
  • 3
  • 18
  • 35
  • Nice idea, but for some reason, I only get the word LOGO printed out where the logo should be. I use this in header.php instead of the_custom_logo(); – Yoav Vollansky Nov 14 '18 at 03:01
1

If your page is SEO Friendly and You change <html lang=''> for different language, then You can change logo on client side by jQuery.

Wordica
  • 2,427
  • 3
  • 31
  • 51
1

I don't recommend doing this in PHP since that would invalidate your cache and in turn will affect your performance. Instead you should use JavaScript, JQuery to change the logo based on your lang atribute.

Edit: It looks like WPML actually allows this by installing the WPML String Translations extension.

EmilCataranciuc
  • 1,024
  • 1
  • 11
  • 24
1
function pojo_polylang_get_multilang_logo( $value ) {
if ( function_exists( 'pll_current_language' ) ) {
    $logos = array(
        'en' => 'logo-en.png',
        'in' => 'logo-in.png',
    );
    $default_logo = $logos['en'];
    $current_lang = pll_current_language();
    $assets_url = get_stylesheet_directory_uri() . '/assets/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' );

if you have a look from $assets_url to $return $value you will see an if statement :

        $assets_url = get_stylesheet_directory_uri() . '/assets/images/';
    if ( isset( $logos[ $current_lang ] ) )
        $value = $assets_url . $logos[ $current_lang ];
    else
        $value = $assets_url . $default_logo;
}
return $value

from what I am looking at I can see an error as the if statement does not include {} maybe if you changed it to the following it might work?

        $assets_url = get_stylesheet_directory_uri() . '/assets/images/';
    if ( isset( $logos[ $current_lang ] ) ) {
        $value = $assets_url . $logos[ $current_lang ];
    } else {
        $value = $assets_url . $default_logo;
    }
}
return $value
dheeraj Kumar
  • 372
  • 3
  • 20
JamesBond
  • 312
  • 2
  • 17