More fixes

WordPress feed or sitemap error: "XML declaration allowed only at the start of the document"

You open /feed/ or /sitemap_index.xml and the browser shows "This page contains the following errors: error on line 2 at column 6: XML declaration allowed only at the start of the document". Feed readers, Mailchimp RSS campaigns and Google Search Console then say the feed or sitemap "could not be read". An XML file must start with <?xml as its very first characters. Something on the site printed a blank line (or a space, or an invisible byte-order mark) before it. The usual source is a theme or plugin file with an empty line before <?php or after a closing ?>. The XML itself is fine; only that stray line has to go.

What the owner sees

harborbakery.example/feed/

What the checks found


  

Find out which case it is (five minutes)

  1. Look at the line number. "Line 2" (or line 3, 4) means one or more blank lines came first. "Line 1, column 4" or similar usually means invisible bytes: a byte-order mark saved by a desktop editor.
  2. See the stray line yourself. Open the feed address with view-source: in front of it (for example view-source:https://yoursite.com/feed/). If the first line is empty and <?xml sits on line 2, that is the fault.
  3. Is it only the feed, or the sitemap too? If both break, the blank line comes from code that runs on every page: the active theme's functions.php, wp-config.php, or a plugin. If only a sitemap breaks, look first at the SEO plugin that makes it and anything that hooks into it.
  4. What changed last? A snippet pasted into functions.php, a hand edit to wp-config.php, or a plugin installed or updated just before the feed broke is the place to start.

The fixes, by case

  1. Take a copy first. Download the files you are about to touch with the host's File Manager or FTP, so every change can be undone.
  2. Check the theme's functions.php: in the host's File Manager, open wp-content/themes/your-theme/functions.php. The very first characters must be <?php, with no blank line or space before them. At the end, delete a closing ?> and any empty lines after it; the file does not need one. Do the same for wp-config.php in the site's main folder.
  3. Re-save without a byte-order mark: if the top looks clean but the error stays, the file was saved with a "BOM". Re-save it as "UTF-8 without BOM" (Notepad++: Encoding, UTF-8; VS Code: click "UTF-8 with BOM" in the status bar, Save with Encoding, UTF-8) and upload it again.
  4. Find a plugin that prints the line: switch to a default theme (Twenty Twenty-Four) in Appearance, Themes and reload the feed. If it still breaks, turn plugins off one at a time (or rename a plugin's folder in wp-content/plugins/ by adding -off) and reload after each. The one that fixes the feed is the culprit: update it, or replace it.
  5. Clear caches, then re-check: purge the caching plugin and any CDN, open view-source: on the feed again, and confirm <?xml is now on line 1. In Search Console, resubmit the sitemap; in a feed reader, refresh the feed.

What you see, what it means, what it needs

What you seeWhat it meansUsual fix
"error on line 2 at column 6"One blank line before <?xmlRemove the empty line before <?php or after ?> in the file that prints it
"error on line 1" with odd characters, or nothing visibleA byte-order mark at the start of a PHP fileRe-save the file as UTF-8 without BOM
Feed and sitemap both brokenCode that runs on every page prints the lineCheck functions.php and wp-config.php, then plugins one by one
Only the sitemap brokenThe SEO plugin or something hooked into itUpdate the SEO plugin, then test the other plugins against it
Search Console: "Sitemap could not be read"Google hit the same stray lineFix the line, then resubmit the sitemap

What not to do

Most cases are one blank line or one re-save in one file. If you cannot find which file prints it, that is what the box below is for. Related: "headers already sent" warning, site not showing on Google and PHP warnings on the site.