Redirect Users to Language Version Based on Location in WordPress

How to redirect users to language based on location; Wordpress redirect language by geolocation; Automatic language redirection wordpress; Geolocation language redirect plugin; Set up language redirect based on user location; Wordpress geolocation language switch; Redirect website visitors to correct language; Language redirection using geolocation wordpress; How to change site language based on location; Wordpress auto language redirect setup;

Explanation

Here's a simple way to automatically redirect your website visitors to the correct language version based on their location. This approach uses geolocation data to determine where your visitors are coming from and redirects them to the appropriate language page.

How It Works:

  • The code hooks into WordPress's early loading process to check the visitor's location.
  • If the visitor is logged in or on an admin page, no redirection occurs to avoid disrupting their session.
  • It checks if a redirection has already happened using a cookie to prevent looping.
  • The visitor's IP address is used to fetch their geolocation data from a third-party service.
  • Based on the visitor's country code, the code checks if there's a corresponding language version available.
  • If a match is found, the visitor is redirected to the appropriate language page.
  • A cookie is set to ensure the redirection doesn't happen repeatedly during the same session.

Things to Note:

  • You need to replace the placeholder API URL with a real geolocation service and API key.
  • Customize the $language_redirects array to include more country codes and language paths as needed.
  • This setup is ideal for sites with multiple language versions organized in subdirectories (e.g., /fr/ for French).

By implementing this, your site will automatically guide users to the content in their preferred language, enhancing their browsing experience.

Code

<?php
// Hook into 'init' to perform the redirection early in the WordPress loading process
add_action('init', 'wp_dudecom_redirect_based_on_geolocation');

function wp_dudecom_redirect_based_on_geolocation() {
    // Check if the user is already logged in or if it's an admin page
    if (is_user_logged_in() || is_admin()) {
        return;
    }

    // Check if the redirection has already been performed to prevent loops
    if (isset($_COOKIE['wp_dudecom_language_redirect'])) {
        return;
    }

    // Use a third-party service to get the user's geolocation
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $geo_data = wp_dudecom_get_geolocation_data($user_ip);

    if (!$geo_data || !isset($geo_data['country_code'])) {
        return;
    }

    // Define language redirection rules based on country codes
    $language_redirects = array(
        'FR' => '/fr/', // Redirect French users to the French version
        'DE' => '/de/', // Redirect German users to the German version
        'ES' => '/es/', // Redirect Spanish users to the Spanish version
        // Add more country codes and corresponding language paths as needed
    );

    $country_code = $geo_data['country_code'];

    // Check if there is a language path for the user's country
    if (array_key_exists($country_code, $language_redirects)) {
        $redirect_url = home_url($language_redirects[$country_code]);

        // Set a cookie to prevent repeated redirections
        setcookie('wp_dudecom_language_redirect', '1', time() + 3600, COOKIEPATH, COOKIE_DOMAIN);

        // Perform the redirection
        wp_redirect($redirect_url);
        exit;
    }
}

function wp_dudecom_get_geolocation_data($ip) {
    // Example using a free geolocation API (replace with a real API key and endpoint)
    $api_url = 'https://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&ip=' . $ip;

    $response = wp_remote_get($api_url);

    if (is_wp_error($response)) {
        return false;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    return $data;
}
?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have a geolocation API service and an API key.
  • Organize your site with language versions in subdirectories (e.g., /fr/ for French).

Implementation Steps:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
  2. Locate the File: Navigate to your theme's directory and open the functions.php file. Alternatively, create or open a custom plugin file if you prefer to keep custom code separate from your theme.
  3. Insert the Code: Copy and paste the provided code into the functions.php file or your custom plugin file.
  4. Replace API Details: Update the $api_url in the wp_dudecom_get_geolocation_data function with your actual geolocation API endpoint and API key.
  5. Customize Language Redirects: Modify the $language_redirects array to include the country codes and corresponding language paths relevant to your site.
  6. Save Changes: Save the changes to the functions.php file or your custom plugin file.
  7. Test the Redirection: Visit your site from different locations (or use a VPN) to ensure users are redirected to the correct language version based on their location.

For further assistance or to implement more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.