Light bulb We updated the website and resources for you. Send Feedback

Fixed Error: WordPress XML Declaration Allowed Only Start of Document

Published 7 Nov, 2022 Modified 30 Oct, 2023 Read in 6m 57s Viewed 5.54K times

Is your sitemap or other XML file in Wordpress throwing XML declaration error? It's not WP error. Something happened on our files end. Lets get into that.

Experience Based

You may have encountered an error like below:

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.

We encountered this error on WP Sitemap /wp-sitemap.xml and SEO Yoast Sitemap XML /sitemap_index.xml URLs. However, in your case it could be other website with or without WP or Yoast.

This XML declaration error appears for any of the following reasons: –

  • You have an empty line, blank line, new line, or space after the closing PHP tag ?> at the end of file in wordpress plugins or themes or your other non-WP site. This is the most common issue.
  • You are using a plugin or script that is altering XML headers incorrectly.
  • You may have spaces, newlines, or other characters before the XML declaration (<?xml version="1.0" encoding="UTF-8"?>). This happens quite frequently in WordPress because of whitespace in functions or in files.

How to Fix Empty Line at End of File Issue in WP

Though this issue is most common issue, however, this is sometimes frustrating to fix. Since it is hard to track which file could be having an empty line at the end of file, it becomes a time taking activity.

Here are steps to troubleshoot and fix the issue:

  1. Check for Whitespace Before the Opening PHP Tag: Ensure that there is no whitespace before the opening <?php tag or after the closing ?> tag in your functions file or any included files.
  2. Avoid Closing PHP Tag: In WordPress, it’s a common practice to leave off the closing ?> tag at the end of PHP files, to ensure that no accidental whitespace is added. So, if your PHP file ends with ?>, just remove it.
  3. Check the Function Outputting the XML: Ensure that nothing is being echoed or printed before the header() function call or before the XML declaration. It should look something like this:
header('Content-Type: application/xml; charset=utf-8'); 
echo '<?xml version="1.0" encoding="UTF-8"?>'; // Rest of your XML content
  1. Check for BOM: Ensure that your PHP files are saved without a Byte Order Mark (BOM). The BOM can be invisible in many editors but could cause output before the XML declaration. If you’re using an editor or IDE, make sure to save your file as “UTF-8 without BOM”.
  2. Disable Plugins: Sometimes a plugin might be the culprit, sending output before it should. Try disabling plugins one by one to see if the issue is resolved, indicating which plugin might be causing the problem.
  3. Check Theme Files: The active theme can be a source of unwanted output. Check your theme’s functions.php file or other included files for stray whitespace or characters outside of PHP tags.
  4. Use Output Buffering: As a last resort, you can start output buffering at the very beginning of the script using ob_start(), then echo your XML content, and finally use ob_end_flush() at the end to send the buffered output. This way, any accidental output will be captured in the buffer and won’t interfere with your XML structure.

header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
// Rest of your XML content


By following these steps, you should be able to identify and resolve the issue causing the XML declaration error.

Checking for Whitespace or New Line at the end of WordPress File

Finding the whitespace or new line in WordPress files can be fixed with divide and conquer rule.

Start with most common files where you or your team edit code.

These are functions.php in child or parent theme, wp-config.php in the WP root directory, and template files (if you have) in child theme.

If you don’t make changes to wp-config.php or functions.php or other template files often, then head towards recently installed plugins. Disable them one by one and check if the XML declaration error goes away.

Common plugins that may produce this error are: –

  • Plugins related to webp or other image format generation (common)
  • Plugins related to caching that modify headers
  • Plugins that adds XML related functionality to the current theme

If you just installed so many plugins or it was your new WP installation with tons of plugins and themes, we recommend using Health Check and Troubleshooting Plugin. This works by disabling extension and themes one by one without affecting your visitors. This is really helpful for custom-built websites since disabling some extensions throw fatal errors most of the time.

The above solution may also fix the following similar WordPress or other PHP Application’s XML related errors: –

  • xml declaration allowed only at the start of the document wordpress
  • yoast error on line 2 at column 6: xml declaration allowed only at the start of the document
  • yoast error on line 2 at column 6: xml declaration allowed only at the