Skip to content

Instantly share code, notes, and snippets.

@2ndkauboy
Last active September 17, 2019 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 2ndkauboy/76d5c5a14df8c0d01d81743880e0e386 to your computer and use it in GitHub Desktop.
Save 2ndkauboy/76d5c5a14df8c0d01d81743880e0e386 to your computer and use it in GitHub Desktop.
<?php
/**
* Language Attribute Variant Fix
*
* @package language-attributes-variant-fix
* @author Bernhard Kau
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: Language Attribute Variant Fix
* Plugin URI: https://kau-boys.de
* Description: Removes the language variant from the laguage attribute used on the HTML root element
* Version: 0.1
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* Strips the language variant
*
* @param string $language_attributes The original language attributes value.
*
* @return string
*/
function language_attributes_variant_fix( $language_attributes ) {
return preg_replace( '/lang="([\w]+-[\w]+)-[\w]+"/', 'lang="$1"', $language_attributes );
}
add_filter( 'language_attributes', 'language_attributes_variant_fix' );
@pixolin
Copy link

pixolin commented Aug 14, 2018

function language_attributes_variant_fix( $language_attributes ) {
        return preg_replace( '/lang="([\w]+)-[\w]+[-\w]*"/', 'lang="$1"', $language_attributes );
}
add_filter( 'language_attributes', 'language_attributes_variant_fix' );

If we change the quantifier for the last segment, the function will work with de-DE als well as de-DE-formal.
I also selected the country code only, following the conversation on Twitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment