Wordpress

HowTo Fix incorrect og:locale in Yoast’s WordPress SEO Plugin with Genesis framework

I noticed that the famous WordPress plugin for SEO by Yoast does not allow a flexible handling of post languages, expecially on this website we’re i’ve mixed language.

Hopefully its easy to fix expecially when using the Genesis theme framework.

In functions.php of your own child theme put this code at the the bottom:

add_filter('wpseo_locale', 'my_wpseo_locale' );

function my_wpseo_locale() {
    $locale = genesis_get_custom_field('locale');
    if (strlen($locale)>=2) return $locale;
    return 'it_IT';
}

What this code do is looking for a custom field called “locale” that you can add to any posts to override the default one.

I choosed it_IT as default just for example but you can do it your own way.

If you’re not using the Genesis framework you need to implement yourself the genesis_get_custom_field function that retrieve the custom field value from the current post or page.

Extend the changes to other plugins

Filtering by hooking the wpseo_locale hook will only affect the Yoast plugin.

If you want to extends those changes to other plugins, for instance the Genesis builtin “eNews” plugin, you have to manipulate the return value of the get_locale() wordpress core function by hooking the locale hook.

You can do this by just replacing:

add_filter('wpseo_locale', 'my_wpseo_locale' );

with

add_filter('locale', 'my_wpseo_locale' );