Back to Community

Fixing the 'XML Declaration Allowed Only at the Start of the Document' Sitemap Error

20 threads Sep 16, 2025 PluginXml sitemap generator for google

Content

If you're seeing an error message like "error on line 2 at column 6: XML declaration allowed only at the start of the document" when trying to view your XML sitemap, you're not alone. This is a common issue reported by users of the 'XML Sitemap Generator for Google' plugin and other sitemap generators. This guide will explain why this happens and walk you through the most effective troubleshooting steps to resolve it.

What Does This Error Mean?

An XML sitemap is a structured document that must follow strict formatting rules. The very first line of the file must be the XML declaration: <?xml version="1.0" encoding="UTF-8"?>. The error occurs when something—most often an invisible blank space or line—is output before this declaration, pushing it down to line 2 or 3. This invalidates the XML structure, causing the error you see.

What Causes the Whitespace or Blank Line?

The extra blank line is almost never caused by the sitemap plugin itself. Instead, it is typically introduced by other PHP files in your WordPress installation. Common culprits include:

  • Extra Whitespace in PHP Files: A single space or blank line before the opening <?php tag or after the closing ?> tag in a theme or plugin file.
  • Common Offenders: The issue is frequently found in your active theme's functions.php file or the site's root wp-config.php file.
  • Plugin Conflicts: A poorly coded plugin might be outputting whitespace.
  • Byte Order Mark (BOM): Some text editors save files with a hidden BOM character at the beginning.
  • Caching/Performance Plugins: In rare cases, these can sometimes interfere with the output.

How to Troubleshoot and Fix the Error

Follow these steps in order to identify and eliminate the source of the unwanted whitespace.

Step 1: Perform a Basic Conflict Check

Before editing any code, this is a quick and safe test to see if another plugin is causing the problem.

  1. Temporarily deactivate all other plugins except 'XML Sitemap Generator for Google'.
  2. Switch your theme to a default WordPress theme like Twenty Twenty-Four.
  3. Check your sitemap URL (e.g., yoursite.com/sitemap.xml). If the error is gone, you know the problem lies with a plugin or your theme.
  4. Reactivate your plugins one-by-one, checking the sitemap after each activation, until you find the one that causes the error to return.
  5. Finally, reactivate your original theme. If the error returns, the issue is in your theme's code.

Step 2: Check Key Files for Whitespace

If the conflict check points to your theme, or you need to investigate further, carefully inspect these files using a code editor (like VS Code or Sublime Text) or the WordPress Theme Editor. Always back up your site before editing files.

A. Check your theme's functions.php file:

  1. Go to Appearance > Theme File Editor in your WordPress admin.
  2. Select your active theme and open the functions.php file.
  3. Look intently at the very top of the file. There must be absolutely nothing—not even a blank space—before the opening <?php tag.
  4. Scroll to the very bottom of the file. Ensure there is nothing—no spaces, no blank lines—after the closing ?> tag. Best practice is to omit the closing ?> tag entirely to prevent this issue.

B. Check your wp-config.php file:

  1. Access this file via FTP/SFTP or your web host's file manager. It is located in the root directory of your WordPress installation.
  2. Open it in a plain text/code editor. Again, ensure there is no whitespace before the opening <?php tag or after any code at the end of the file.

Step 3: Use a Diagnostic Script (Advanced)

If you cannot find the source of the whitespace, a community-recommended diagnostic script can help. The script at wejnswpwhitespacefix.php.html can be uploaded to your server to track down which file is outputting the blank characters. Use this method with caution and only if you are comfortable working with server files.

Step 4: Check for Encoding Issues

As seen in one thread, an "Encoding error" can also break the sitemap. This is often caused by special characters copied from word processors into post content. If you see encoding errors, try to identify and remove or replace any non-standard characters in your posts or pages.

Important Note: Search Engines

It's worth noting that, in many cases, search engines like Google can still read and process sitemaps that have this whitespace error. However, fixing it is still recommended for validation and to ensure full compatibility with all web services.

Need More Help?

If you've tried these steps and are still encountering issues, the best course of action is to seek help from the wider WordPress community. You can describe the steps you've already taken and provide a link to your sitemap URL for others to inspect. Often, a fresh set of eyes can quickly spot the problem.

Related Support Threads Support