Home > Networking > How I Fixed the “XML Declaration Allowed Only at the Start of the Document” Error in Yoast SEO Sitemap

How I Fixed the “XML Declaration Allowed Only at the Start of the Document” Error in Yoast SEO Sitemap

How I Fixed the “XML Declaration Allowed Only at the Start of the Document” Error in Yoast SEO Sitemap

If you’re using Yoast SEO to generate a sitemap for your WordPress site and you’ve run into the frustrating “error on line 2 at column 6: XML declaration allowed only at the start of the document” message, you’re not alone. I recently encountered this issue myself, and after some digging and trial-and-error, I found a solution that worked like a charm. In this post, I’ll walk you through the steps I followed to fix it—hopefully, it’ll save you some time and headaches!

What Causes This Error?

This error typically pops up when there’s unwanted whitespace (like blank lines or spaces) at the beginning of your XML sitemap file. It can happen due to a plugin, theme, or even a stray line in your WordPress files adding extra output before the XML declaration. In my case, it was breaking my sitemap and preventing search engines from reading it properly.

The Fix: Adding a Whitespace Cleanup Script

After scouring the internet for solutions, I found a reliable fix that involves adding a small PHP script to remove that pesky whitespace. Here’s how I did it, step by step:

Step 1: Log into cPanel

First, I logged into my hosting account’s cPanel. If you’re not sure how to do this, check with your hosting provider—they’ll usually provide login details or a direct link.

Step 2: Navigate to Your WordPress Root Directory

Once in cPanel, I opened the File Manager and navigated to my WordPress root directory. This is usually the folder where you see files like wp-config.php, index.php, and the wp-content folder. For most setups, it’s in public_html or a subdirectory if you’re running multiple sites.

Step 3: Create a New File Called whitespacefix.php

In the File Manager, I clicked “New File” and named it whitespacefix.php. Then, I opened the file and pasted the following code:

php

<?php
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");
?>

This script checks for whitespace at the start of the output and removes it, but only for specific content types like XML, ensuring it doesn’t interfere with other parts of your site.

Step 4: Edit index.php

Next, I opened the index.php file in the same root directory. Right after the opening <?php tag, I added this line:

php

include('whitespacefix.php');

So, the top of my index.php looked something like this:

php

<?php
include('whitespacefix.php');
// Define ABSPATH and load WordPress...

I saved the file, and that was it!

Step 5: Test Your Sitemap

After making these changes, I went back to my WordPress dashboard, cleared any caches (if you’re using a caching plugin), and regenerated the Yoast SEO sitemap. I then visited yoursite.com/sitemap_index.xml in my browser. Success—no more error! The sitemap loaded perfectly.

Why This Works

The whitespacefix.php script hooks into PHP’s output buffer and strips out any leading whitespace before the XML declaration, which is what was causing the issue. By including it in index.php, it runs on every page load but only affects the output when necessary.

A Quick Note

This fix worked for me, but every WordPress setup is unique. If you’re still seeing issues, double-check your theme or plugins for extra output (like blank lines in functions.php). And, as always, back up your site before making changes—just in case!

Wrapping Up

Fixing the Yoast SEO sitemap error was a bit of a puzzle, but this solution got me back on track. Have you run into this issue before? How did you solve it? Let me know in the comments—I’d love to hear your tips! And if this guide helped you, feel free to share it with others who might be struggling with the same problem.

Find the root cause:

Actually, the above just workaround. For the actual root cause please make sure no space before <?php in wp-config.php in your wordPRess root directory.

The normal config file should be like the reference below.

Leave a Comment