Fixing WordPress sitemap.xml error on line at column

WordPress sitemap.xml: This page contains the following errors: error on line 3 at column 6: XML declaration allowed only at the start of the document

WordPress sitemap.xml: This page contains the following errors: error on line 3 at column 6: XML declaration allowed only at the start of the document

Working on a WordPress site I kept seeing this error on my sitemap.xml “This page contains the following errors: error on line 3 at column 6: XML declaration allowed only at the start of the document” which basically messed up the sitemap producing invalid XML. I was using a plugin to generate the sitemap, Google XML Sitemaps, so at first I thought that might be the issue so I tested another one, Better WordPress Google XML Sitemaps, but it was also messed up with the same error.

I’ve been working on a number of WordPress sites using this same plugin without seeing this error so I pulled up some working sitemaps along with the broken version. A quick comparison of the source code for each sitemap.xml and I immediately noticed the culprit at the top of the broken sitemap there where two blank blank lines aka white space at the very top. We know this is the issue because the error specifially mentions line 3 which is the first line after the white space ends. Check out my screenshot of the XML source:

WordPress sitemap.xml white space at top

WordPress sitemap.xml white space at top

Fixing the WordPress white space issue above XML

A search of the Google and I quickly found multiple posts discussing the issue. It’s not just the sitemap that is messed up but all of the XML generated by WordPress like RSS feeds.  Most recommend activating a different theme (like Twenty Fifteen or any that come by default) and disable plugins to try to find what is creating the white space. Honestly, that might work for some but I was using the same base theme and plugin on a different site with the sitemap.xml working so I did not want to waste time with that.

Most forum discussions lead to a 2010 blog post: WordPress Remove Blank Line From RSS Feed – wordpress error on line 2 at column 6: XML declaration allowed only at the start of the document. I recommend reading this post to learn more about this issue because it’s well written and informative but it’s not very up to date so I had to modify the directions a bit to get it working on modern WordPress.

Basically you need to download a small PHP script/file called wejnswpwhitespacefix.php and include that in your WordPress. The original post includes the script at the top of the index.php file but that did not actually work for me on WordPress version 4.2.5 so that original method may not work anymore for latest versions. Plus, adding it to index.php is not the best place because that gets updated for new version of WordPress so you would have to make the change each time you update the core. Some people had also commented down that page that the index.php method was not working for 4.0 so I recommend using the alternate version below.

Include the script wejnswpwhitespacefix.php into your theme’s functions.php

I added the file wejnswpwhitespacefix.php to my theme folder and then included it using my theme’s functions.php file. Every WordPress theme should have this file with code already in it most likely so all you need to do is add this one line at the very top of the file functions.php right under the <?php tag:

<?php
include("wejnswpwhitespacefix.php");
//the rest of the functions.php like before below, the original code already in it

Save that and you’re done! Make sure the wejnswpwhitespacefix.php and your functions.php are in the same folder else you will need to change the path to that file. Now hitting refresh the sitemap.xml works along with RSS feeds and any other XML WordPress generates! Here is a screenshot of the valid XML from this very site sans the white space at top like it should be.

WordPress sitemap.xml valid not white space

WordPress sitemap.xml valid wihout white space http://andrew.technology/sitemap.xml

Posted in Code, Issues, PHP, WordPress Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*