Last updated on October 18, 2023

WPML canonical URLs for multilingual sites

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Manage canonical URLs for multilingual sites with WPML.

The code snippet below demonstrates how to add WPML canonical URLs for multilingual sites in WordPress. This is useful for ensuring that search engines understand the relationship between different language versions of your content and avoid duplicate content issues.

function wpsnippets_wpml_canonical_url( $url ) {
    if ( defined( 'ICL_LANGUAGE_CODE' ) && function_exists( 'icl_object_id' ) ) {
        $post_id = icl_object_id( get_the_ID(), 'post', true, ICL_LANGUAGE_CODE );
        $url = get_permalink( $post_id );
    }
    return $url;
}
add_filter( 'wpml_canonical_url', 'wpsnippets_wpml_canonical_url' );

In this code, we define a custom function wpsnippets_wpml_canonical_url that takes the current URL as a parameter. Inside the function, we check if the WPML language code is defined (ICL_LANGUAGE_CODE) and if the icl_object_id function exists. If both conditions are met, we retrieve the translated post ID using icl_object_id and then get the permalink for that post using get_permalink. Finally, we return the translated URL.

To enable this functionality, we use the wpml_canonical_url filter hook and add our custom function wpsnippets_wpml_canonical_url as the callback function using add_filter.

By adding this code to your theme’s functions.php file or a custom plugin, WPML will generate canonical URLs based on the translated versions of your content, helping search engines understand the relationship between different language versions and improving SEO for your multilingual site.

Examples

Example 1: Adding WPML canonical URLs to the head section of a WordPress site

This code example adds the WPML canonical URLs to the head section of a WordPress site. It ensures that search engines understand the relationship between different language versions of a page and helps prevent duplicate content issues.

function wpsnippets_add_wpml_canonical_url() {
    if (function_exists('icl_object_id')) {
        $current_language = ICL_LANGUAGE_CODE;
        $current_url = $_SERVER['REQUEST_URI'];
        $canonical_url = icl_get_home_url($current_language) . $current_url;
        echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />';
    }
}
add_action('wp_head', 'wpsnippets_add_wpml_canonical_url');

Explanation: This code checks if the WPML plugin is active and retrieves the current language code and URL. It then constructs the canonical URL using the icl_get_home_url() function and echoes it within a <link> tag in the head section of the site.

Example 2: Customizing WPML canonical URLs for specific post types

This code example demonstrates how to customize the WPML canonical URLs for specific post types. It allows you to define different canonical URLs based on the post type, providing more control over the canonicalization process.

function wpsnippets_custom_wpml_canonical_url($canonical_url) {
    if (is_singular('product')) {
        $product_id = get_the_ID();
        $product_permalink = get_permalink($product_id);
        $canonical_url = $product_permalink;
    }
    return $canonical_url;
}
add_filter('wpml_canonical_url', 'wpsnippets_custom_wpml_canonical_url');

Explanation: This code uses the wpml_canonical_url filter to modify the canonical URL generated by WPML. In this example, it checks if the current page is a singular post of the ‘product’ post type. If so, it retrieves the product ID and permalink, and sets the canonical URL to the product permalink.

Example 3: Excluding specific pages from WPML canonical URLs

This code example demonstrates how to exclude specific pages from WPML canonical URLs. It allows you to prevent certain pages from being included in the canonicalization process, which can be useful for landing pages or custom templates.

function wpsnippets_exclude_pages_from_wpml_canonical($exclude_pages) {
    $excluded_page_ids = array(10, 15, 20); // IDs of the pages to exclude
    $exclude_pages = array_merge($exclude_pages, $excluded_page_ids);
    return $exclude_pages;
}
add_filter('wpml_exclude_pages_from_canonical', 'wpsnippets_exclude_pages_from_wpml_canonical');

Explanation: This code uses the wpml_exclude_pages_from_canonical filter to exclude specific pages from WPML canonical URLs. In this example, it adds the page IDs (10, 15, and 20) to the array of excluded pages, ensuring that these pages are not included in the canonicalization process.

Last updated on October 18, 2023. Originally posted on October 31, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *