Skip to content

Instantly share code, notes, and snippets.

@guydumais
Last active October 25, 2021 16:42
Show Gist options
  • Save guydumais/21b00532068a1ff2318706e146d76665 to your computer and use it in GitHub Desktop.
Save guydumais/21b00532068a1ff2318706e146d76665 to your computer and use it in GitHub Desktop.
WordPress ➧ Change Locale Dynamically in WordPress
/**
* Change Locale Dynamically in WordPress.
*
* @author Guy Dumais.
* @link https://en.guydumais.digital/change-locale-dynamically-in-wordpress/
*/
// Filters all language attributes in the html tag.
add_filter( 'language_attributes', $af = function( $output, $doctype ) {
if ( strpos($output, 'en-US') !== false ) {
$output = str_replace('en-US', 'en-CA', $output);
}
return $output;
}, 10, 2 );
unset( $af );
// Filters all language attributes in social tags (og:).
add_filter( 'locale', $af = function( $locale ) {
if( 'en_US' == $locale ) {
$locale = 'en_CA';
}
return $locale;
}, 10 );
unset( $af );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment