Menü schliessen
Created: November 26th 2024
Last updated: November 26th 2024
Categories: Php,  Wordpress
Author: Aleksandar Pantic

Creating a Multi-Language WordPress Site Using PHP

Tags:  PHP,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Why Build a Multi-Language WordPress Site?

  • Attract users from different language backgrounds.
  • Improve SEO by targeting region-specific keywords.
  • Enhance user experience by providing content in multiple languages.

Step 1: Setting Up Your WordPress Installation

Make sure your WordPress installation is up to date. For this example, we’ll focus on a single WordPress installation, but you could also use WordPress Multisite for a more complex multi-language setup.

Step 2: Creating a Custom Language Selector

function custom_language_selector() {
    echo 'English | ';
    echo 'Español | ';
    echo 'Français';
}
add_action('wp_footer', 'custom_language_selector');

Step 3: Handling Translations Using PHP

function set_language_cookie() {
    if (isset($_GET['lang'])) {
        $lang = $_GET['lang'];
        setcookie('language', $lang, time() + 3600, '/');
        $_COOKIE['language'] = $lang;
    }
}
add_action('init', 'set_language_cookie');

function display_translated_content($content) {
    if (isset($_COOKIE['language'])) {
        $lang = $_COOKIE['language'];
        
        if ($lang == 'es') {
            $content = str_replace('Welcome to my website', 'Bienvenido a mi sitio web', $content);
        } elseif ($lang == 'fr') {
            $content = str_replace('Welcome to my website', 'Bienvenue sur mon site Web', $content);
        }
    }
    
    return $content;
}
add_filter('the_content', 'display_translated_content');

Step 4: Translating Pages and Posts

function get_translated_title($post_id) {
    if (isset($_COOKIE['language'])) {
        $lang = $_COOKIE['language'];
        $translated_title = get_post_meta($post_id, 'post_title_' . $lang, true);
        
        if ($translated_title) {
            return $translated_title;
        }
    }
    
    return get_the_title($post_id);
}

echo get_translated_title(get_the_ID());

Step 5: Storing and Retrieving Translated Content

function get_translated_post($post_id) {
    if (isset($_COOKIE['language'])) {
        $lang = $_COOKIE['language'];
        $translated_post_id = get_post_meta($post_id, 'translated_post_' . $lang, true);
        
        if ($translated_post_id) {
            return get_post($translated_post_id);
        }
    }
    
    return get_post($post_id);
}

Step 6: Optimizing for SEO

function custom_meta_tags() {
    if (isset($_COOKIE['language'])) {
        $lang = $_COOKIE['language'];
        
        // Define meta title and description based on language
        if ($lang == 'es') {
            $meta_title = 'Título en español';
            $meta_description = 'Descripción en español';
        } elseif ($lang == 'fr') {
            $meta_title = 'Titre en français';
            $meta_description = 'Description en français';
        } else {
            $meta_title = 'English Title';
            $meta_description = 'English Description';
        }

        // Dynamically set the title and description in the head
        add_filter('pre_get_document_title', function($title) use ($meta_title) {
            return $meta_title;
        });
        
        add_filter('wpseo_metadesc', function($description) use ($meta_description) {
            return $meta_description;
        });
    }
}
add_action('wp_head', 'custom_meta_tags');

Step 7: Testing and Troubleshooting

Once you’ve implemented the multi-language functionality, thoroughly test your site:

  • Check that the language switcher works correctly.
  • Ensure the correct translations are displayed based on the selected language.
  • Test for any issues with cookie management or URL parameters.

Conclusion

Creating a multi-language WordPress site using PHP offers a high level of customization and control. By using PHP and WordPress's built-in features, you can manage translations, customize the content display, and create a user-friendly language switcher. While plugins like WPML and Polylang simplify the process, a custom approach ensures flexibility and greater control over your multilingual content. With careful planning, coding, and testing, you can create a powerful multi-language site that serves a global audience.