Fixed Error: WordPress XML Declaration Allowed Only Start of Document
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.
In adherence to our rigorous editorial policy, this article's content has undergone careful testing for accuracy and trustworthiness and hence, this content is marked reliable source of information. View editorial history of this content.
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:
- Check for Whitespace Before the Opening PHP Tag: Ensure that there is no whitespace before the opening
<?phptag or after the closing
?>tag in your functions file or any included files.
- 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.
- 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
- 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”.
- 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.
- Check Theme Files: The active theme can be a source of unwanted output. Check your theme’s
functions.phpfile or other included files for stray whitespace or characters outside of PHP tags.
- 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.
ob_start(); header('Content-Type: application/xml; charset=utf-8'); echo '<?xml version="1.0" encoding="UTF-8"?>'; // Rest of your XML content ob_end_flush();
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.
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
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
Digital Setups has enforced a strict sourcing policy. Every content piece published on our website is passed through strict editorial review for contextual correctness, communication ethics, and programmatic tests wherever required. Our team research solutions from only credible, authentic, and trustworthy sources. Learn more about our editorial process.
- WordPress PHP Coding Standards" developer.wordpress.org - WordPress.org, https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#remove-trailing-spaces Accessed October 30, 2023
- FAQ: UTF-8, BOM, and PHP" www.w3.org - World Wide Web Consortium (W3C), https://www.w3.org/International/questions/qa-utf8-bom Accessed October 30, 2023
- Debugging in WordPress" developer.wordpress.org - WordPress.org, https://developer.wordpress.org/themes/theme-development/debugging-in-wordpress/ Accessed October 30, 2023
- Output Control Functions" www.php.net - The PHP Group, https://www.php.net/manual/en/ref.outcontrol.php Accessed October 30, 2023
Based on our editorial policy, we update our content time to time to ensure its usefulness, reliability, and validity.
Our standardized editorial process ensures right, timely, and usefulness updates to our content. Your honest opinion drives significant improvement to our content. We appreciate you are taking time to share that.
Readers who read this also found these helpful:
- Does Hiding Actual Content with Captchas Impact SEO?
- Does daily manual or dynamically changing content on page impact SEO?
- Research-Driven eCommerce Approach: Learned from CEO Talha Iftikhar
- IPO Trademark Search in Pakistan
- Block Editor: theme.json – Register Patterns
- Fixed: WordPress wp-json 500 Server Error
- Take back your business name used by someone on Social Media
- Search Console: Fix “Alternate page with proper canonical tag” Error in Page Indexing
- Apache Access Log Entry: CONNECT Method
- Impact of Google BARD AI on Search Traffic to Websites