1️⃣ Introduction
When it comes to SEO, an XML sitemap plays a crucial role in helping search engines like Google and Bing discover and index your website’s content properly and faster.
However, many WordPress sites encounter the common error:
“XML declaration allowed only at the start of the document.”
If this happens, Google Search Console will flag your sitemap, SEO tools like Semrush will report syntax errors, and your indexing speed may drop.
👉 This is a real troubleshooting experience that SinhMMO successfully handled and now shares with you – so you can fix it yourself in under 15 minutes!

2️⃣ Root causes (real examples)
After deep investigation, this sitemap error usually comes from three main reasons:
✅ a) Extra spaces in core files
- Your
wp-config.php
orfunctions.php
file may have blank lines before the opening<?php
tag or unnecessary?>
at the end. - This is a tiny mistake but extremely common when editing files directly.
✅ b) Snippet or plugin outputs echo
- PHP snippets (added via WPCode, Code Snippets) sometimes contain
echo
outside of a proper hook. - If placed incorrectly, this generates extra output before the XML declaration.
- Plugins like Ad Inserter, GTranslate, or Shortcodes Ultimate can conflict if they modify headers.
✅ c) SEO plugin conflict
- Using Yoast SEO or RankMath with other plugins that also output header content can create duplicate or misplaced XML output.
- Custom child themes or nulled themes can also cause messy code.
3️⃣ How to detect this error
✔️ Check your sitemap URL:
- Visit
https://yourdomain.com/sitemap_index.xml
➜ If you seeerror on line 2 at column 6
, the issue exists.
✔️ Google Search Console:
- Re-submit your sitemap ➜ If it shows “Couldn’t fetch”, you know there’s a syntax problem.
✔️ Use Semrush or Ahrefs:
- Run a crawl audit ➜ Look under Sitemaps or Structured Data sections.
✔️ View page source:
- Right-click the sitemap ➜ View source ➜ If you see any spaces before
<?xml
, you’ve found the culprit!

4️⃣ Step-by-step solution
Here’s exactly how SinhMMO fixed the issue and kept it clean forever:
✅ Step 1: Fix wp-config.php
- Open the file ➜ Make sure there’s nothing before
<?php
. - Remove any
?>
at the end. - Save & clear your cache.
✅ Step 2: Fix functions.php
- Open your child theme’s
functions.php
. - Add at the top:
<?php ob_start();
- Make sure there’s no
echo
outside of hooks.
✅ Step 3: Access your hosting server and create a new file
- You can name this file
whitespacefix.php
or any name you prefer<?php function ___wejns_wp_whitespace_fix($input) { $allowed = false; $found = false; foreach (headers_list() as $header) { if (preg_match("/^content-type:\\s+(text\\/|application\\/((xhtml|atom|rss)\\+xml|xml))/i", $header)) { $allowed = true; } if (preg_match("/^content-type:\\s+/i", $header)) { $found = true; } } if ($allowed || !$found) { return preg_replace("/\\A\\s*/m", "", $input); } else { return $input; } } ob_start("___wejns_wp_whitespace_fix"); ?>
✅ Step 4: Include this file in your index.php
Open the index.php
file and add the following line right below <?php
:
✅ Step 5: Re-submit to Google
- In Search Console ➜ Remove the old sitemap ➜ Submit your new one.
- Wait for Google to process it ➜ It should show Success.
5️⃣ Results & key takeaways
After this fix:
- The XML sitemap loads 100% valid.
- Semrush and other SEO tools show no syntax errors.
- New posts get indexed faster.
- The website runs smoother with no header conflicts.
📌 Pro tips
- Always backup your site before editing core files.
- Use trusted plugins only.
- Add a clear
robots.txt
:User-agent: * Allow: / Sitemap: https://sinhmmo.com/sitemap_index.xml
- Regularly audit your sitemap with Search Console.
✅ Conclusion
👉 This is the exact method SinhMMO used to fix a real-life sitemap XML error.
If you face the same issue, follow these steps, or 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