XML Sitemap Error on line 2 at column 6
XML Sitemap Error on line 2 at column 6: The error you’re encountering, “XML declaration allowed only at the start of the document”, typically occurs when there’s extra whitespace or an unexpected character before the XML declaration (<?xml version="1.0" encoding="UTF-8"?>
) in your XML sitemap.
Here’s how you can troubleshoot and resolve the issue:
1. Check for Blank Spaces or Line Breaks in Theme or Plugin Files
Sometimes, extra spaces or new lines added in your theme’s functions.php
file, or in one of your plugins, can cause this issue.
- Open your
functions.php
file (or any custom plugin file you’ve recently edited). - Look for any whitespace or line breaks before the opening
<?php
tag. - Ensure that the
<?php
tag is the very first thing in the file, with no spaces or line breaks above it.
<?php
// Your code goes here
- Repeat this check for any custom or third-party plugins that might affect the XML sitemap.
2. Disable Plugins Temporarily
A plugin might be generating unwanted output. You can disable your plugins temporarily and then check if the sitemap works.
- Go to Plugins > Installed Plugins.
- Deactivate your plugins one by one, checking your sitemap after each deactivation. If the sitemap starts working after disabling a particular plugin, you’ve found the culprit.
- Focus on plugins related to SEO, caching, or sitemap generation (e.g., Yoast SEO, Rank Math, etc.).
3. Create a File in Root Directory
create a new php file and name it whatever you want eg “whitespaces.php” and insert below codes in that php file
<?php
// www.designinggeeks.com/ || Please Support & Subscribe our Youtube Channel ||
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");
?>
After creating the file, open index.php file and include the file you just created.write below code after <php tag.
include ('whitespaces.php');
Let us know if you need further assistance!
Aute mi ut suspendisse velit leo, vel risus ac. Amet dui dignissim fermentum malesuada auctor volutpat, vestibulum ipsum nulla.
Sed reprehenderit quam, non felis, erat cum a, gravida lorem a. Ultricies in pellentesque ipsum arcu ipsum ridiculus velit magna, ut a elit est. Ultricies metus arcu sed massa. Massa suspendisse lorem turpis ac.
Massa suspendisse lorem turpis ac. Pellentesque volutpat faucibus pellentesque velit in, leo odio molestie, magnis vitae condimentum.