If statement does not working in WordPress

Hey I am trying to make a if statement, which define the language which is been using in the website first, and then depends on the language, choosing the right content to show up.

Here is the code

Read More
[insert_php]$mylocale = get_bloginfo('language');if ($mylocale=="fr-FR"){[/insert_php]
        <div>Fr</div>[insert_php]}else{[/insert_php]
        <div>Eng</div>[insert_php] } [/insert_php]

WordPress does not allow PHP inside pages or posts, so I use a plugin called “insert PHP” to test the code inside a page, therefore I am using [insert_php] instead of <?php ?>.
But the problem is the if statement does not work. it always shows both of “Eng” and “Fr” without picking the right one.

I also tried

[insert_php]$mylocale = get_bloginfo('language');if($mylocale=="fr-FR"):[/insert_php]
<div>Fr</div>[insert_php]; else:[/insert_php]
<div>Eng</div>[insert_php]; endif;[/insert_php]

Does not work as well.

Anyone can help me please? Thank you

Related posts

Leave a Reply

1 comment

  1. Due to the nature of the plugin, you’re going to have to wrap the entire statement in the shortcode tags:

    [insert_php]
        $mylocale = get_bloginfo('language');
        if ($mylocale=="fr-FR") {
            echo '<div>Fr</div>';
        } else {
            echo '<div>Eng</div>';
        } 
    [/insert_php]
    

    However, from the docs , you should be aware of the following:

    The code between [insert_php] and [/insert_php] must be complete in
    and of itself. The block of code is fed into the eval() function,
    where the PHP code is run.

    References to variables, functions, or code blocks outside the area
    between [insert_php] and [/insert_php] will fail because the code
    being eval()’d has no access to the outside. In other words, no
    outside scope is possible.

    It’s likely you won’t have access to get_bloginfo() because it is outside of the scope, rendering all of this impossible.