
Sửa lỗi sitemap: XML declaration allowed only at the start of the document
Nếu bạn dùng plugin SEO như Rank math hoặc Yoast SEO và tạo sitemap để submit cho Google đọc dữ liệu nhưng 1 ngày đẹp trời bạn truy cập vào sitemap https://domain-cua-ban.com/sitemaps.xml và gặp lỗi như sau.
This page contains the following errors:
error on line 2 at column 11: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
line 2 và column 11 thì có thể thay đổi do tuỳ theo đoạn code, nhưng tổng thể là lỗi XML declaration allowed only at the start of the document thì bạn có thể xử lý nhanh theo cách dưới đây.
Bạn cần đăng nhập vào hosting vào thư mục cài đặt website WordPress (thư mục gốc chứa source WP) sau đó tạo mới 1 file có tên là: fixsitemap.php
sau đó dán code phía dưới vào file vừa tạo.
<?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");
Tiếp theo, bạn hãy mở file index.php
(fix index.php nó nằm ngay thư mục bạn vừa tạo file fixsitemap.php
luôn nhé)
Bài viết liên quan
Tiếp theo, bạn hãy mở file wp-config.php
Nếu bạn không biết file wp-config.php ở đâu bạn có thể đọc qua bài này nhé
Tiếp đó thêm vào sau thẻ mở <?php
code sau:
define('INCLUDE_SITEMAP_FIX', true); if (defined('INCLUDE_SITEMAP_FIX') && INCLUDE_SITEMAP_FIX) { require_once(__DIR__ . '/fixsitemap.php'); }
Ban đầu tôi hướng dẫn thêm vào file index.php nhưng nếu bạn cập nhật wp core thì chúng sẽ bị ghi đè và sitemap sẽ bị lỗi, nên cách ở trên sẽ giải quyết vấn đề triệt để hơn nhé.
Sau khi add code sitemap sẽ load bình thường
Hy vọng bài viết nhanh sẽ giúp ích được cho nhiều người gặp phải vấn đề khi tạo sitemap
Xin cảm ơn.
Code tham khảo: https://wejn.org/stuff/wejnswpwhitespacefix.php.html