I was working on a new client's website today, checked sitemap_index.xml and saw this 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.
The XML sitemap was rendered using the Yoast SEO plugin. This is what it should look like:
Viewing the source of the page, the error was being caused by an errant space at the top of the XML sitemap.
The problem isn't caused by the Yoast SEO plugin. It's brought in from another plugin or theme.
The actual cause is a bit weird. It's because of an errant space at the top (or possibly bottom) of a php file.
It can be a bit tricky to figure out which file is causing the problem.
How to fix the Yoast Sitemap XML Declaration Error:
- Move a copy of the site to a development server
- Disable all the plugins other than Yoast SEO
- Switch the theme to Twenty Twenty
- Activate the plugins and theme one by one to see which is causing the error
- Go through the files, looking for a blank line before the <?php declaration
- Apply the fix to the live website
Move a copy of the site to a development area
Unless you have no one visiting the site, you don't want to be doing this on a live website.
If you can't migrate the website, or don't want to, then at least put the website into maintenance mode while you work on this. Otherwise it will be a terrible experience for your visitors.
Until right at the end of this guide, everything will be done on the copy of the website.
Open the Yoast XML Sitemap on the copy of the website. If it's setup the same as the original website, the sitemap on the copy of the website should also be showing the XML declaration error. If it's not, the problem must be with something on the host. You should contact them to resolve it.
Assuming the copy of the website has the declaration error, keep the sitemap open in a separate tab. As you turn things off, you'll want to refresh the sitemap to see if you've located the problem.
Disable all the plugins other than Yoast SEO
In the WordPress admin area of the copy of the website, go to Plugins -> Installed Plugins.
Check the checkbox next to "Plugin" to select all the plugins on the website. Scroll down and uncheck Yoast SEO.
At this point, everything other than the Yoast SEO plugin is checked.
If you're doing this on a live website with maintenance mode active, make sure to uncheck the maintenance mode plugin too.
In the Bulk Actions menu, choose "Deactivate" and click "Apply".
This will deactivate all the plugins that you checked.
Refresh the Yoast XML sitemap now to see if that fixed the issue.
If it did fix the issue, turn the plugins on one by one. Refresh the sitemap after each to see which one causes the sitemap to break.
Just in case there is more than one plugin causing the error, deactivate the one you know is broken and activate the rest. Repeat the detective work if necessary.
When you find the problematic plugin, you'll need to decide whether to replace the plugin with something else, or contact the plugin developer to get them to fix it.
Switch the theme to Twenty Twenty
If you've gotten to this point, the problem is probably with the theme.
Start by turning off any plugins that might cache the website. You don't want to have to be dealing with that while running these tests.
If you're running a child theme, it could be on either the child or parent.
To confirm that this is the case, change the theme to Twenty Twenty (or any of the default WordPress themes that come preinstalled).
To do this, go to Appearance -> Themes. Hover over Twenty Twenty and click "Activate".
Refresh the sitemap. The sitemap should be working by now.
If you are using a child theme, just activate the parent theme.
Refresh the sitemap. If it's still working, the problem is in the child theme. If it's not, you'll have to look into the parent theme.
Start by looking at the functions.php file.
You're looking for a blank line above the <?php line, which should start the file.
It's fine for there to be anything else on the same line after <?php, but nothing should be before it on that line, nor should there be anything above it.
If you find what you think is the problem, fix it, save it and refresh the sitemap.
Also look through functions.php for any theme files that are included or required. One of those was the problem I encountered today.
Including or requiring a file will look like one of these lines:
include (get_template_directory() . '/whatever.php');
require (get_template_directory() . '/however.php');
include_once (get_template_directory() . '/whomever.php');
require_once (get_template_directory() . '/wherever.php');
The key part is the first statement of each of those lines. It just tells WordPress to refer to those other files.
Try commenting out those lines by adding a double slash to the start of the line like so:
//include_once (get_template_directory() . '/whomever.php');
Save and refresh the sitemap.
You might get some errors, because other parts of the functions.php are relying on those includes and requires.
If those errors are affecting the sitemap display (other than the declaration error), try to comment out all the add_action and add_filter lines (just use the bulk find and replace in your text editor).
Hopefully by this stage you should know which file is causing the issue.
If there is no extra space at the top of that file, check the end of the file. I've not found that extra space at the end of php files has caused this type of issue, but in figuring out this issue today I read that sometimes this causes the error.
Apply the fix to the live website
Once you've applied the fix (clear the cache if you have one active), refresh the sitemap. Hopefully it's now working fine and you can resubmit it to Google.
If the sitemap is still not working, check whether you have any Must-Use or Drop-in plugins on the website:
If you do, you will probably have to raise the issue with your host as it may be an issue with one of their automatically installed plugins.