【问题标题】:How can I get the WordPress language_attributes function to return valid XHTML 1.1?如何让 WordPress language_attributes 函数返回有效的 XHTML 1.1?
【发布时间】:2009-11-26 21:24:20
【问题描述】:

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

<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 中是无效的 - 并且客户端想要这种级别的验证。

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

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

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

我也尝试过使用 preg_replace 取出“lang”属性,但这似乎无法匹配文本。如果我手动输入文本,它匹配!可能是 language_attributes 返回的字符串的编码问题?

【问题讨论】:

    标签: php wordpress w3c-validation xml-attribute xhtml-1.1


    【解决方案1】:

    我解决了这个问题。有一个“language_attributes”过滤器,所以我写了一个plugin 来连接它并执行一个简单的preg_replace。替换在这里执行时有效,这是一种非常巧妙的处理方式。

    编辑

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

    <?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');
    ?>
    

    【讨论】:

      【解决方案2】:

      如果这只是您自己网站上的主题,您可以编辑 header.php 并更改

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

      要硬编码的行,也可以提高性能 :-)

      【讨论】:

      • 这是一个选项。但我不确定客户端可能会将模板用于其他什么用途,因此,如果可以,我想保留该功能。
      猜你喜欢
      • 2021-01-05
      • 2010-11-03
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2015-02-14
      • 2017-03-27
      相关资源
      最近更新 更多