One way to optimize SEO for a multilingual WordPress site using WPML is by adding hreflang tags to your website’s pages. These tags help search engines understand the language and regional targeting of your content, which can improve your site’s visibility in different languages and regions.
To add hreflang tags to your WordPress site using WPML, you can use the wpml_add_language_selector()
function. This function generates the necessary hreflang tags based on the languages and translations set up in WPML.
Here’s an example of how you can use the wpml_add_language_selector()
function to add hreflang tags to your site’s header:
function wpsnippets_add_hreflang_tags() {
if (function_exists('wpml_add_language_selector')) {
wpml_add_language_selector(array('skip_missing' => 0, 'link' => 'hreflang'));
}
}
add_action('wp_head', 'wpsnippets_add_hreflang_tags');
This code snippet adds the wpml_add_language_selector()
function to the wp_head
action hook, which ensures that the hreflang tags are added to the header of your site’s pages.
By using this code snippet, you can optimize the SEO of your multilingual WordPress site by adding hreflang tags, which helps search engines understand the language and regional targeting of your content.
Examples
Example 1: Adding Multilingual SEO Meta Tags
This use case demonstrates how to add multilingual SEO meta tags to your WordPress site using WPML. The code example below shows how to add a custom function that hooks into the wp_head
action to add the necessary meta tags for each language.
function wpsnippets_add_multilingual_seo_meta_tags() {
if (function_exists('wpml_get_current_language')) {
$current_language = wpml_get_current_language();
if ($current_language == 'en') {
echo '<meta name="description" content="English description">';
} elseif ($current_language == 'fr') {
echo '<meta name="description" content="French description">';
}
}
}
add_action('wp_head', 'wpsnippets_add_multilingual_seo_meta_tags');
This code checks the current language using the wpml_get_current_language
function and adds the appropriate meta tag based on the language. You can customize the meta tags and their content for each language.
Example 2: Translating SEO-friendly URLs
This use case demonstrates how to translate SEO-friendly URLs for different languages using WPML. The code example below shows how to use the wpml_translate_single_string
function to translate the slug of a custom post type.
function wpsnippets_translate_custom_post_type_slug($slug, $post_type) {
if ($post_type == 'product') {
$translated_slug = wpml_translate_single_string('wpml', 'product_slug_' . $slug, $slug);
if ($translated_slug) {
return $translated_slug;
}
}
return $slug;
}
add_filter('wp_unique_post_slug', 'wpsnippets_translate_custom_post_type_slug', 10, 2);
This code hooks into the wp_unique_post_slug
filter and checks if the post type is ‘product’. It then uses the wpml_translate_single_string
function to translate the slug based on the current language. If a translation is found, it returns the translated slug; otherwise, it returns the original slug.
Example 3: Translating SEO Metadata
This use case demonstrates how to translate SEO metadata, such as title and description, for different languages using WPML. The code example below shows how to use the wpml_translate_single_string
function to translate the title and description of a post.
function wpsnippets_translate_seo_metadata($metadata, $post_id, $meta_key, $single) {
if ($meta_key == '_yoast_wpseo_title' || $meta_key == '_yoast_wpseo_metadesc') {
$translated_metadata = wpml_translate_single_string('wpml', 'seo_metadata_' . $metadata, $metadata);
if ($translated_metadata) {
return $translated_metadata;
}
}
return $metadata;
}
add_filter('get_post_metadata', 'wpsnippets_translate_seo_metadata', 10, 4);
This code hooks into the get_post_metadata
filter and checks if the meta key is ‘yoastwpseotitle’ or ‘yoastwpseometadesc’. It then uses the wpml_translate_single_string
function to translate the metadata based on the current language. If a translation is found, it returns the translated metadata; otherwise, it returns the original metadata.