【分享】关于修复WordPress站点地图.xml“第3列第6行的错误”

这是我自己在检查站点SEO的时候发现的一个小问题,不知道其他人有没有出现,做一下记录,希望能帮到遇到同类问题的小伙伴。

图片[1]-【分享】关于修复WordPress站点地图.xml“第3列第6行的错误”-五喜丸日常
This page contains the following errors:
error on line 3 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

通过查询发现可能的原因是:①插件与主题冲突;②wp-config.php或主题文件functions.php文件的开头(<?php行之前)或结尾的空行

经过排查空行发现,应该是第二个原因

修复方法

在主题目录下创建一个空白的wuxi.php文件,然后将以下代码复制粘贴进去。

<?php
/*函数___wejns_wp_whitespace_fix的主要逻辑如下:
*初始化变量$allowed和$found,它们用于确定是否允许修复空白字符。
*使用headers_list()函数遍历所有的HTTP响应头部信息。
*如果某个响应头部信息的内容类型(Content-Type)匹配正则表达式/^content-type:s+(text/|application/((xhtml|atom|rss)+xml|xml))/i,则将*变量$allowed设置为true,表示允许修复空白字符。
*如果某个响应头部信息以Content-Type开头,则将变量$found设置为true,表示找到了Content-Type头部信息。
*如果$allowed为true或者没有找到Content-Type头部信息,则使用正则表达式/As* /m将输入的内容中的开头空白字符替换为空字符串。
*否则,直接返回输入的内容。
*最后,通过ob_start("___wejns_wp_whitespace_fix")函数,将修复空白字符的函数应用于输出缓冲区,以确保输出中的空白字符被修复。
*/


function ___wejns_wp_whitespace_fix($input) {
	$allowed = false;

	$found = false;

	foreach (headers_list() as $header) {
		if (preg_match("/^content-type:\\s+(text\\/|application\\/((xhtml|atom|rss)\\+xml|xml))/i", $header)) {
			$allowed = true;
		}

		if (preg_match("/^content-type:\\s+/i", $header)) {
			$found = true;
		}
	}

	
	if ($allowed || !$found) {
		return preg_replace("/\\A\\s*/m", "", $input);
	} else {
		return $input;
	}
}


ob_start("___wejns_wp_whitespace_fix");
?>

在主题目录functions.php文件中的第一行“<?php”之后添加“include(”wuxi.php“);”,添加后的样式如下:

<?php
include("spacefix.php");
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

    暂无评论内容