1️⃣ Introduction
An XML sitemap is very important for SEO. It helps Google and Bing discover and index your website faster.
But sometimes, WordPress sites face this error:
“XML declaration allowed only at the start of the document.”
When this happens:
-
Google Search Console flags your sitemap.
-
SEO tools like Semrush or Ahrefs report syntax issues.
-
Your indexing speed slows down.
👉 This guide is based on a real fix from SinhMMO. You can solve it yourself in under 15 minutes.

2️⃣ Root Causes
This sitemap error usually comes from three common problems:
a) Extra spaces in core files
-
wp-config.php
orfunctions.php
may contain blank lines before the<?php
tag. -
Sometimes developers leave
?>
at the end of files. -
Even a single space can break your XML.
b) Wrong snippet or plugin output
-
PHP snippets added via WPCode or Code Snippets may use
echo
outside of hooks. -
Plugins like Ad Inserter, GTranslate, or Shortcodes Ultimate sometimes output headers incorrectly.
-
This creates unwanted content before the XML starts.
c) SEO plugin conflicts
-
Yoast SEO, RankMath, or All in One SEO can conflict with other plugins.
-
Custom or nulled themes may also output duplicate XML.
3️⃣ How to Detect the Error
Here are simple ways to confirm the problem:
-
Check your sitemap URL: Visit
https://yourdomain.com/sitemap_index.xml
. If you see an error on line 2, the issue exists. -
Google Search Console: Re-submit your sitemap. If it says “Couldn’t fetch”, there’s a syntax error.
-
SEO tools: Run a site audit with Semrush or Ahrefs. Look for sitemap errors.
- View source: Right-click ➜ View page source. If you see spaces before
<?xml
, that’s the culprit.

5️⃣ Results & Key Takeaways
After applying the fix:
- Your sitemap becomes valid.
- SEO tools no longer show errors.
- New posts index faster.
- Your site runs smoother without header conflicts.
📌 Pro Tips
- Always back up your site before editing.
- Use only trusted plugins.
- Add a clean
robots.txt
:
User-agent: *
Allow: /
Sitemap: https://yourdomain.com/sitemap_index.xml
- Audit your sitemap regularly with Search Console.
✅ Conclusion
This is the exact method SinhMMO used to fix a real-life XML sitemap error.
👉 If you face the same issue, follow these steps.
And if you still get stuck, drop a comment on SinhMMO.com — we’re always ready to help.
🎉 Happy fixing! May your site get indexed lightning-fast.
HI
I fixed success