Could we help you? Please click the banners. We are young and desperately need the money
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.
function custom_language_selector() {
echo 'English | ';
echo 'Español | ';
echo 'Français';
}
add_action('wp_footer', 'custom_language_selector');
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');
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());
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);
}
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');
Once you’ve implemented the multi-language functionality, thoroughly test your site:
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.