Xử lý lỗi Sitemap: XML declaration allowed only at the start of the document

Nếu các bạn truy cập Sitemap của website liên kếthttps://domain.com/sitemap_index.xmlvà gặp lỗi như ảnh trên, thì đừng quá lo lắng. Cảnh báo lỗi này nghĩa là website đang gặp phải các lỗi trống hay khoảng trắng tại vị trí được cảnh báo (error on line 3 at column 6). Để khắc phục nó, bạn chỉ cần làm theo các bước bên dưới là được nhé.

Bước 1: Tạo file whitespacefix.php

Đầu tiên, bạn hãy đăng nhập vào Hosting/VPS của bạn. Tiếp đó, bạn di chuyển vào đúng đường dẫn chứa dữ liệu website và tạo mới một File mới và đặt tên làwhitespacefix.php
Tiếp đó bạn hãy chọn Edit File vừa tạo, và thêm đoạn code vào nhé:
<?phpfunction ___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");?>

Bước 2:  Include vào index.php

Tiếp đó, cũng tại đường dẫn chứa dữ liệu website bạn chọn Edit Fileindex.phpvà thêm đoạn bên dưới vào:
include('whitespacefix.php');
Sau khi đã include xong, bạn hãy Save File lại và truy cập lại Sitemap với liên kếthttps://domain.com/sitemap_index.xmlđể kiểm tra kết quả. Và kết quả là mình đã xử lý được lỗi Sitemap rồi nhé.
Chúc các bạn thực hiện thành công.!
Thêm đánh giá post

Pages