WordPress August 16, 2023

Sitemap.xml Issue and Solution

Author: Seçil Durgun

What is a Sitemap?

A sitemap is a critical tool that systematically lists all the pages of a website, allowing search engines to gain comprehensive information about each page’s content, associated links, and structural layout. This file is fundamental in optimizing the search engine’s process of discovering the website while enhancing the user experience. Sitemaps provide a holistic view of your site by revealing hidden or hard-to-reach pages, enabling search engines to index your website more effectively and deliver more accurate and detailed results to users. A sitemap helps search engines detect a website more efficiently and thoroughly.

Sitemaps are typically written in XML format and submitted to search engines via webmaster tools. They can contain information such as the frequency of page updates, the priority of the pages, and alternative language versions of a page.

Having a sitemap is essential, especially for large websites with many pages, as it facilitates search engines’ discovery and indexing of all pages, thereby increasing the website’s visibility in search results.

How to Fix a WordPress Sitemap Error?

In this article, I will explain how to fix the following error:
This page contains the following errors: error on line 2 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. “

To fix this error, you need to follow the steps below:

1. Open a new file using an editing tool.

2. Paste the code provided below into the file and save it as whitespacefix.php.

3. Upload the saved file to your root directory.

<?php
function ___tasarlab_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("___tasarlab_wp_whitespace_fix");

4. Open the index.php file in the root directory with your editor and insert the code below right after <?php.

include('whitespacefix.php');
5. Then save and close the index file.

The sitemap error has now been fixed. The code above removes the theme’s spaces and blank lines.

What Causes This Error?

Any whitespace or blank lines before the XML DOCTYPE declaration cause this error. This whitespace or blank line may be caused by a caching plugin or other plugins you are using.

In our case, as you can see when viewing the source code of the sitemap.xml page, there is a space and some blank lines before the start of the XML tag (<?).

Conclusion

I hope you fixed this sitemap error. If you encounter any issues with your sitemap, contact us, and we can help you.