【備忘録】WordPressの「Google XML Sitemaps」プラグインでエラー

IT技術系

WordPressの「Google XML Sitemaps」プラグインを入れたらエラーが出てしまったので、備忘録を残しておきます。

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

調べてみると、xml宣言が先頭に無いことがいけないようです。
出力ファイルのソースを確認します。

確かに2行目から出力されています。なぜなのか…

原因は、作成した子テーマのfunction.phpファイルでした。

最後の行に改行が入ってる。これを消すと直りました。

2020/07/11追記:それでも直らなかったのですが、下記で直りました。

①ここに私が見つけた解決策があります。まず、次の内容でワードプレスルートディクトリにphpファイル(whitespacefix.php)を作成します。

<?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”);
?>

②次に、index.phpファイルを開き、<?phpタグの直後に次の行を追加します

include(‘whitespacefix.php’);