WordPress visits the corresponding webpage according to the browser language

WordPress is used as a CMS, and sometimes it is necessary to allow visitors to only access specific web pages based on different languages, mainly to determine the browser value: HTTP_ACCEPT_LANGUAGE

There are many such judgments on the Internet, but if you define “else” to access the website itself, you will fall into an endless loop. Open index.php in the root directory (not modify Index.php in the template directory) and modify it as follows:

<?php
$lang = substr($_SERVER[‘HTTP_ACCEPT_LANGUAGE’], 0, 2);
if($lang==”zh”) header(“location: https://www.bkzzz.com/cn-index.htm”);//The capitalized code here is to add
/**
 * Front to the WordPress application. This file doesn’t do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define(‘WP_USE_THEMES’, true);/** Loads the WordPress Environment and Template */
require(‘./wp-blog-header.php’);
?>

The explanation is as follows:

  1. Take the first two digits of SERVER[‘HTTP_ACCEPT_LANGUAGE’, determine if it is ZH, it is simplified or traditional Chinese, and all jump to CN-INDEX.htm.
  2. Pay attention to the usage of the hearder function. The sub-function can only be placed at the top, and no other language can be run before the function is run. There can be no spaces between the location and the colon (:), pay special attention.

When revising this question, it is easy to find that the original website is misplaced, please check my other post.

Another problem is the browser type. Generally, it is judged by the value of $_SERVER[“HTTP_USER_AGENT. The above code has been tested in IE6 and IE8. I don’t know if other browsers will be useful. If anyone has tested it, please tell me [email protected], thank you very much. If you can’t use it in other browsers, you need to add $_SERVER[“HTTP_USER_AGENT to judge.

Remarks about other codes found on the Internet. I did a test and it can’t be used, but you can refer to:

<?php error_reporting(E_ALL ^ E_NOTICE); // analysis HTTP_ACCEPT_LANGUAGE Attribute of

// Here only take the first language setting (other functions can be enhanced as needed, here is only a simple way to demonstrate)

preg_match(‘/^([a-z\-]+)/i’, $_SERVER[‘HTTP_ACCEPT_LANGUAGE’], $matches);
$lang = $matches;
switch ($lang) {
       case ‘zh-cn’ :
               header(‘Location: https://www.bkzzz.com/cn-index.htm’);
               break;
       default:
               header(‘Location: http://www.bkzzz.com’);
               break;
}
?>

The above is not interrupted. It is estimated that the default can also be used after removing the default. If there are other languages that need to be judged, you can continue to add the CASE format. This method is to judge all the values, instead of taking two digits, you should also add traditional Chinese characters, etc.

<?php
$lang = substr($_SERVER[‘HTTP_ACCEPT_LANGUAGE’], 0, 2);
 switch ($lang){
      case “zh”:         //echo “PAGE FR”;
       include(“www.bkzzz.com/cn-index.htm”);//include check session FR        
    break;
    default:         //echo “PAGE EN – Setting Default”;      
    include(“www.bkzzz.com/index.htm”);//include EN in all other cases of different lang detection      
    break; }
?>

The above is not interrupted, it is estimated that the default can also be used after removing the default

<?php
$lang = substr($_SERVER[‘HTTP_ACCEPT_LANGUAGE’], 0, 2);
if($lang==”zh”) header(“location:cn-index.htm”);
else header(“location:www.bkzzz.com”);

?>

The above is not interrupted, so everything after else is removed.

<?php
$lang = substr($_SERVER[‘HTTP_ACCEPT_LANGUAGE’], 0, 2);
 switch ($lang){
      case “zh”:  //echo “PAGE FR”;
       header(location:http://www.bkzzz.com/cn-index.htm);//include check session FR        
    break;
    default:         //echo “PAGE EN – Setting Default”;      
    header(location:http://www.bkzzz.com/index.htm);//include EN in all other cases of different lang detection      
    break; }
?>

Same problem, don’t interrupt. After removing the default.

Leave a Comment