Selecteur de langue

I really like the Slim SEO plugin, if you’re looking for a good simple plugin for your SEO needs that integrates well with WordPress without bloating your dashboard and drowning you in ads, go for it!

I recently published the English version of my site to make it more accessible and to suit my future needs. My breadcrumb is managed with Slim SEO, and I’ve encountered a small problem: the link to the home page always points to the root of the site, in my case the French version. I use Polylang for multilingual, and despite the integration of Slim SEO, I don’t feel that the problem has been fixed.

As I couldn’t find a solution that suited me, here’s mine for changing the link to the home page.

The idea

  1. Intercept the breadcrumb links generated by Slim SEO, we’ve got a hook for that.
  2. Replace the URL of the first entry (the home page) with the one translated into the current language, if Polylang is active.
  3. Return modified links so that they are displayed correctly in the current language.

Code extract


/**
 * Modify Slim SEO breadcrumb links to support Polylang-translated front page URLs.
 *
 * Replaces the URL of the first breadcrumb link with the correct language-specific front page URL
 * using Polylang's `pll_get_post()` and `pll_current_language()`.
 *
 * @param array $links Array of breadcrumb links.
 * @return array Modified breadcrumb links with language-specific home URL.
 */

add_filter('slim_seo_breadcrumbs_links', function($links) {
    if (empty($links)) {
        return $links;
    }

    if (function_exists('pll_current_language')) {
        $lang = pll_current_language();
        $front_page_id = get_option('page_on_front');
        $home_page_id = function_exists('pll_get_post') ? pll_get_post($front_page_id, $lang) : $front_page_id;

        if ($home_page_id) {
            $home_url = get_permalink($home_page_id);

            $links[0]['url'] = $home_url;
        }
    }

    return $links;
});
Code language: PHP (php)

I hope this post helps you. If you have any questions, need support on your WordPress site, or want to contribute to improve the code snippet, don’t hesitate to contact me or leave a comment!

Author

Quentin Le Duff: Your WordPress partner

Comments

Leave a Reply

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