当前浏览:首页 / WordPress钩子 / language_attributes

apply_filters( 'language_attributes', string $output, string $doctype )

过滤“html”标签中显示的语言属性

attributes

language


参数

$outputstring
用空格分隔的语言属性列表。
$doctypestring
HTML文档的类型(xhtml|html)。


源码

查看源码 官方文档


更新日志

版本描述
4.3.0添加了$doctype参数。
2.5.0开始引入

使用示例

  • 示例1

    下面是一个函数,用于强制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;
    	} );
    }