1

i need to create a shortcode to display a different Div depending on WP current language only when users are logout.

The shortcode is this one:

function coupon_box() {

$user = is_user_logged_in() ;


if ($user == false) {

    if (get_locale() == 'en_Us') {
    echo ' <div class="discount"><h3 style="text-align: left;">Is this your first order?</h3><p style="text-align: left;">Request your discount code for a value of 20 euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/welcome-coupon/">Receive my code 
</a></span></p></div>';
}

    elseif (get_locale() == 'fr_Fr') {

    echo '<div class="discount"><h3 style="text-align: left;">C’est votre première commande?</h3><p style="text-align: left;">Demandez votre code de réduction d’une valeur de 20 Euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://atremoplus.fr/bienvenue/">Recevoir mon code</a></span></p></div>';
    }

    elseif (get_locale() == 'de_De') {
    echo '<div class="discount"><h3 style="text-align: left;">Ist dies Ihre erste Bestellung?</h3><p style="text-align: left;"> Fordern Sie Ihren persönlichen 20 Euros Rabatt.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/de/atremoplus-kaufen/">Gutscheincode anfordern</a></span></p></div>';
    }

    else {

    echo '<div class="discount"><h3 style="text-align: left;"> ¿Este es su primer pedido?</h3><p style="text-align: left;"> Solicite su código de descuento por un valor de 20 euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/es/comprar-atremoplus/">Recibir mi código</a></span></p></div>';
    }
}

}

add_shortcode('coupon', 'coupon_box');

Is there something you see that is wrong please?

Thank you!

  • By the way it doesn't work – WebMate Oct 26 '18 at 7:26
  • get_locale() will return a value like this en_US. Don't forget the second capital letter – Wilco Oct 26 '18 at 7:44
  • by the way, what are you trying to do with your last else? Is it a error? Warning? Because it's missing get_locale and it's shown to logged in user. – Remzi Cavdar Oct 26 '18 at 7:45
  • That's for the last language, who is spanish. Actually it's not show to logged in users right? – WebMate Oct 26 '18 at 7:48
  • I have edited your code, see below. Now backup language is English and shown to user who are not logged in. You could switch this to any language. If you want Spanish as backup language. – Remzi Cavdar Oct 26 '18 at 8:01
0

In a shortcode you always need to return something. It's also good practice to make your code very logical and readable. Here are a list of PHP Logical Operators: http://php.net/manual/en/language.operators.logical.php Complete List of WordPress Locale Codes: https://wpastra.com/docs/complete-list-wordpress-locale-codes/

Below an example as English as a backup/fallback language:

function coupon_shortcode() {
    $logged_in = is_user_logged_in();

    if (get_locale() == 'fr_FR' && $logged_in == false) {
        return '<div class="discount"><h3 style="text-align: left;">C’est votre première commande?</h3><p style="text-align: left;">Demandez votre code de réduction d’une valeur de 20 Euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://atremoplus.fr/bienvenue/">Recevoir mon code</a></span></p></div>';
    }
    elseif (get_locale() == 'de_DE' && $logged_in == false) {
        return '<div class="discount"><h3 style="text-align: left;">Ist dies Ihre erste Bestellung?</h3><p style="text-align: left;"> Fordern Sie Ihren persönlichen 20 Euros Rabatt.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/de/atremoplus-kaufen/">Gutscheincode anfordern</a></span></p></div>';
    }
    elseif (get_locale() == 'es_ES' && $logged_in == false) {
        return '<div class="discount"><h3 style="text-align: left;">¿Este es su primer pedido?</h3><p style="text-align: left;"> Solicite su código de descuento por un valor de 20 euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/es/comprar-atremoplus/">Recibir mi código</a></span></p></div>';
    }
    elseif ($logged_in == false) { // Backup/fallback language is English and is shown only when user is not logged in
        return '<div class="discount"><h3 style="text-align: left;">Is this your first order?</h3><p style="text-align: left;">Request your discount code for a value of 20 euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/welcome-coupon/">Receive my code 
</a></span></p></div>';
    }
}
add_shortcode('coupon', 'coupon_shortcode');

Below an example as Spanish as a backup/fallback language:

function coupon_shortcode() {
    $logged_in = is_user_logged_in();

    if (get_locale() == 'en_US' && $logged_in == false) {
        return '<div class="discount"><h3 style="text-align: left;">Is this your first order?</h3><p style="text-align: left;">Request your discount code for a value of 20 euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/welcome-coupon/">Receive my code
    </a></span></p></div>';
    }
    elseif (get_locale() == 'fr_FR' && $logged_in == false) {
        return '<div class="discount"><h3 style="text-align: left;">C’est votre première commande?</h3><p style="text-align: left;">Demandez votre code de réduction d’une valeur de 20 Euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://atremoplus.fr/bienvenue/">Recevoir mon code</a></span></p></div>';
    }
    elseif (get_locale() == 'de_DE' && $logged_in == false) {
        return '<div class="discount"><h3 style="text-align: left;">Ist dies Ihre erste Bestellung?</h3><p style="text-align: left;"> Fordern Sie Ihren persönlichen 20 Euros Rabatt.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/de/atremoplus-kaufen/">Gutscheincode anfordern</a></span></p></div>';
    }
    elseif ($logged_in == false) { // Backup/fallback language is Spanish and is shown only when user is not logged in
        return '<div class="discount"><h3 style="text-align: left;">¿Este es su primer pedido?</h3><p style="text-align: left;"> Solicite su código de descuento por un valor de 20 euros.</p><p style="text-align: left;"><span style="text-decoration: underline;"><a class="myButton" href="https://www.atremoplus.net/es/comprar-atremoplus/">Recibir mi código</a></span></p></div>';
    }
}
add_shortcode('coupon', 'coupon_shortcode');
  • Thank you very much for your explication ;-). It works well right now ;-) – WebMate Oct 26 '18 at 9:31
  • You're welcome :) I'm glad it worked. – Remzi Cavdar Oct 26 '18 at 11:00

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.