WordPress PHP

Implementing Multilingual Pages in WordPress PHP

This article introduces how to implement multilingual-compatible pages in WordPress PHP. If you install plugins, all pages become multilingual. I've tried it too, but it became troublesome and I deleted it. I want to do it in PHP like, if Japan, this HTML, if English-speaking countries, this HTML. Moreover, if there aren't many pages to make multilingual and it's just a few articles, I especially don't want to install plugins. So...

Shou Arisaka
3 min read
Nov 13, 2025

This article introduces how to implement multilingual-compatible pages in WordPress PHP.

If you install plugins, all pages become multilingual. I’ve tried it too, but it became troublesome and I deleted it. I want to do it in PHP like, if Japan, this HTML, if English-speaking countries, this HTML. Moreover, if there aren’t many pages to make multilingual and it’s just a few articles, I especially don’t want to install plugins.

So here’s the code.


$exp = function ($v) { return $v; };

function GetIP()
{
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
    {
        if (array_key_exists($key, $_SERVER) === true)
        {
            foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
            {
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
                {
                    return $ip;
                }
            }
        }
    }
}

function multilanguageaboutme($the_content) {
    // if (is_single() && in_category(array('プログラミング'))) {

    if ( !is_page() && get_the_ID()=='10') {

      $return = "";
      $return .= $the_content;

    global $exp ;

if ( shell_exec("curl \"https://ipapi.co/{$exp(GetIP())}/json/\" | python -c 'import json,sys;print json.load(sys.stdin)[\"country_name\"]' | perl -pe \"chomp\"") == "Japan" ){
  // echo "Japan" ;

$return .= <<<'Japanese'

<p>

Nice to meet you. I'm Fumiya.

</p>

Japanese;

} else {
  // echo "Not Japan" ;

$return .= <<<'English'

<p>

Nice to meet you. I'm Fumiya.

</p>

English;

}

        return $return;

  } else {

    $return = $the_content ;
    return $return;

  }

}

// enable/disable
add_filter('the_content','multilanguageaboutme');

Put this code in functions.php or somewhere.

As you can see, it identifies which country it is by IP address, and if the return value is japan, it returns Japanese HTML, otherwise it returns English.

When accessing via VPN

Image

When accessing from Japan

Image

get_the_ID()==‘10’ is the post ID. Let’s make only the title for this post and leave the body blank.

shell_exec("curl \"https://ipapi.co/{$exp(GetIP())}/json/\" | python -c 'import json,sys;print json.load(sys.stdin)[\"country_name\"]' | perl -pe \"chomp\"") is a bit complex, but it’s just executing an external command.

You can do it as much as you want with PHP alone, but since I only work with bash, I’m receiving results from external commands. Anyway, the number of accesses is known, and if it’s about tens of thousands, it won’t be a burden at all. Plus it’s a rental server. If you get an email, just respond.

Digression

By the way, I just researched and learned that when creating multilingual sites, it’s better for SEO to separate URLs. Apparently Google says so. It’s also not good to use JS or cookies to branch and change the display. I kind of knew that.

But I think it’s okay to use this kind of approach if it’s just the top page or the about page in two languages.

Share this article

Shou Arisaka Nov 13, 2025

🔗 Copy Links