如何让WordPress language_attributes函数返回有效的XHTML 1.1?
-
05-07-2019 - |
题
我有一个包含以下元素的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返回的字符串的编码问题?
解决方案
我解决了这个问题。有一个<!> quot; language_attributes <!> quot;过滤器,所以我写了一个插件,它挂钩并执行一个简单的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');
?>
其他提示
如果这只是您自己网站上的主题,您可以编辑header.php并更改
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>>
要硬编码,也要提高性能: - )
不隶属于 StackOverflow