参数
$output
string-
用空格分隔的语言属性列表。
$doctype
string-
HTML文档的类型(
xhtml|html
)。
源码
更新日志
版本 | 描述 |
---|---|
4.3.0 | 添加了$doctype 参数。 |
2.5.0 | 开始引入 |
使用示例
下面是一个函数,用于强制HTML lang属性的区域设置,而不破坏其他属性,如dir
/** * Forces the site locale for the HTML lang attribute. * * @param string $locale the locale to set with the following format : "en-US" * @return void */ function wpdocs_force_site_locale( $locale = 'en-US' ) { add_filter( 'language_attributes', function( $output ) use ( $locale ) { // match the lang attribute and replace it with the new locale $lang_regex = '/lang="([a-zA-Z-_]+)"/'; $output = preg_replace( $lang_regex, 'lang="' . $locale . '"', $output ); // update the xml:lang attribute as well $xml_lang_regex = '/xml:lang="([a-zA-Z-_]+)"/'; $output = preg_replace( $xml_lang_regex, 'xml:lang="' . $locale . '"', $output ); return $output; } ); }