如何让 WordPress language_attributes 函数返回有效的 XHTML 1.1?

发布于 2024-08-12 18:48:27 字数 769 浏览 6 评论 0原文

我有一个包含以下元素的 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 返回的字符串可能存在编码问题?

I have a WordPress template that contains the following element:

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

This returns:

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

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

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

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

$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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-08-19 18:48:27

我解决了这个问题。有一个“language_attributes”过滤器,所以我编写了一个 插件 挂钩到该过滤器并执行简单的 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');
?>

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.

EDIT

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');
?>
人心善变 2024-08-19 18:48:27

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

>

行进行硬编码,也提高了性能:-)

If this is just a theme on your own site, you could edit header.php and change the

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

line to be hardcoded, improves performance too :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文