如何获得WordPress的language_attributes函数以返回有效的XHTML 1.1? [英] How can I get the WordPress language_attributes function to return valid XHTML 1.1?

查看:130
本文介绍了如何获得WordPress的language_attributes函数以返回有效的XHTML 1.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下元素的WordPress模板:

I have a WordPress template that contains the following element:

<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>>

这将返回:

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en-US">

不幸的是,"lang"属性是无效的XHTML 1.1-客户端希望进行此级别的验证.

Unfortunately the "lang" attribute is invalid XHTML 1.1 - and the client would like this level of validation.

WordPress的general-template.php文件包含以下代码:

WordPress' general-template.php file contains the following code:

if ( get_option('html_type') == 'text/html' || $doctype == 'html' )
    $attributes[] = "lang=\"$lang\"";

$doctype是传递给它的参数(在本例中为'xhtml'). get_option是否应该返回'text/html'以外的值?如果是这样,我应该在WordPress中设置什么来实现这一目标-如果有的话?

$doctype is the parameter passed to it (in this case 'xhtml'). Should get_option be returning a value other than 'text/html'? If so, what should I be setting in WordPress to achieve this - if anything?

我也尝试过使用preg_replace去掉"lang"属性,但是这似乎不能匹配文本.如果我手动输入文本,它将匹配!可能是language_attributes返回的字符串存在编码问题?

I've also tried using preg_replace to take out the "lang" attribute, but this didn't seem to be able to match the text. If I enter the text manually, it matches! Possibly an encoding issue with the string being returned by language_attributes?

推荐答案

我解决了这个问题.有一个"language_attributes"过滤器,所以我写了一个插件,它与此挂钩并执行了一个简单的preg_replace.在这里执行替换时,替换就可以了,这是一种很好的处理方式.

I solved this. There's a "language_attributes" filter, so I wrote a plugin that hooks into that and does a simple preg_replace. The replace worked when performed here, and it's a pretty neat way to handle it.

编辑

根据要求,这是我使用的代码:

As requested, here's the code I used:

<?php
/*
Plugin Name: Create Valid XHTML 1.1
Plugin URI: http://www.mycompany.com/create_valid_xhtml_1_1
Description: Removes deprecated "lang" attribute from (X)HTML header.
Author: dommer
Version: 1.0.0
Author URI: http://www.mycompany.com
*/

function create_valid_xhtml_1_1($language_attributes) 
{
    return preg_replace('/ lang=\"[a-z]+\-[A-Z]+\"/', '', $language_attributes);
}

add_filter('language_attributes', 'create_valid_xhtml_1_1');
?>

这篇关于如何获得WordPress的language_attributes函数以返回有效的XHTML 1.1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆