White space issues in WordPress files, “error on line 2 at column 6: XML declaration allowed only at the start of the document” especially in functions.php and wp-config.php, can cause the readed “Headers already sent” error. This happens when there are spaces, line breaks, or invisible characters before the opening <?php tag or after the closing ?> tag.
Manual Fix (Recommended)
Before using a script, check these files manually:
- Access your WordPress files via cPanel File Manager or FTP.
- Open
wp-config.phpandfunctions.php(located in/wp-content/themes/your-theme/). - Remove any spaces or blank lines before
<?phpand after?>(if the closing tag exists). - Save the files and check if the issue is resolved.
Automated Fix Using a Script
If manual removal doesn’t work, you can use a PHP script to clean up white spaces:
Step 1: Create a Fixer Script
- In cPanel File Manager, navigate to your WordPress root directory (where
wp-config.phpis located). - Create a new file named
whitespaceremove.phpand paste this code:
<?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");
?>Then open the index.php and add the line include('whitespaceremove.php'); right after the <?php tag

This answer is very helpful for me. I research this result last 4 days but not resolve. Thanks again.