To translate Yoast SEO titles and meta tags using WPML, you can use the wpseo_title
and wpseo_metadesc
filters provided by the Yoast SEO plugin. These filters allow you to modify the title and meta description output.
Here’s an example code snippet that demonstrates how to translate Yoast SEO titles and meta tags using WPML:
function wpsnippets_translate_yoast_seo_titles($title) {
if (function_exists('wpml_translate_single_string')) {
$translated_title = wpml_translate_single_string('yoast', $title, 'your-translation-group');
if (!empty($translated_title)) {
return $translated_title;
}
}
return $title;
}
add_filter('wpseo_title', 'wpsnippets_translate_yoast_seo_titles');
function wpsnippets_translate_yoast_seo_metadesc($metadesc) {
if (function_exists('wpml_translate_single_string')) {
$translated_metadesc = wpml_translate_single_string('yoast', $metadesc, 'your-translation-group');
if (!empty($translated_metadesc)) {
return $translated_metadesc;
}
}
return $metadesc;
}
add_filter('wpseo_metadesc', 'wpsnippets_translate_yoast_seo_metadesc');
In this code snippet, we define two custom functions wpsnippets_translate_yoast_seo_titles
and wpsnippets_translate_yoast_seo_metadesc
. These functions check if the WPML plugin is active and then use the wpml_translate_single_string
function to translate the Yoast SEO title and meta description strings. The translated strings are returned if they exist, otherwise, the original strings are returned.
We then use the add_filter
function to hook these custom functions to the wpseo_title
and wpseo_metadesc
filters respectively. This ensures that the translations are applied to the Yoast SEO titles and meta descriptions.
Remember to replace 'your-translation-group'
with the appropriate translation group slug for your specific use case.
This code snippet can be useful when you have a multilingual website powered by WPML and want to provide translated titles and meta descriptions for your Yoast SEO plugin. It allows you to customize the SEO content for different languages, improving the SEO optimization and user experience for each language.
Examples
Example 1: Translating Yoast SEO Titles with WPML
This use case demonstrates how to translate Yoast SEO titles using the WPML plugin. The code example shows how to retrieve the translated title for a specific post or page.
$title = get_post_meta( get_the_ID(), '_yoast_wpseo_title', true );
$translated_title = apply_filters( 'wpml_translate_single_string', $title, 'Yoast SEO', 'Title', get_locale() );
echo $translated_title;
In this code example, we first retrieve the Yoast SEO title for the current post or page using the get_post_meta()
function. Then, we use the wpml_translate_single_string
filter provided by WPML to translate the title. The translated title is stored in the $translated_title
variable and can be displayed using echo
.
Example 2: Translating Yoast SEO Meta Descriptions with WPML
This use case demonstrates how to translate Yoast SEO meta descriptions using the WPML plugin. The code example shows how to retrieve the translated meta description for a specific post or page.
$meta_description = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
$translated_meta_description = apply_filters( 'wpml_translate_single_string', $meta_description, 'Yoast SEO', 'Meta Description', get_locale() );
echo $translated_meta_description;
In this code example, we first retrieve the Yoast SEO meta description for the current post or page using the get_post_meta()
function. Then, we use the wpml_translate_single_string
filter provided by WPML to translate the meta description. The translated meta description is stored in the $translated_meta_description
variable and can be displayed using echo
.
Example 3: Translating Yoast SEO Open Graph Tags with WPML
This use case demonstrates how to translate Yoast SEO Open Graph tags using the WPML plugin. The code example shows how to retrieve the translated Open Graph tag value for a specific post or page.
$og_tag_value = get_post_meta( get_the_ID(), '_yoast_wpseo_opengraph-description', true );
$translated_og_tag_value = apply_filters( 'wpml_translate_single_string', $og_tag_value, 'Yoast SEO', 'Open Graph Tag', get_locale() );
echo $translated_og_tag_value;
In this code example, we first retrieve the Yoast SEO Open Graph tag value for the current post or page using the get_post_meta()
function. Then, we use the wpml_translate_single_string
filter provided by WPML to translate the Open Graph tag value. The translated value is stored in the $translated_og_tag_value
variable and can be displayed using echo
.