![Sử dụng chức năng wp_parse_id_list() trong WordPress Sử dụng chức năng wp_parse_id_list() trong WordPress](https://vutruso.com/wp-content/uploads/2023/09/parse-unique-id-480x300.jpg)
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-cu-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 sau đó tạo mới 1 file có tên là: whitespacefix.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 whitespacefix.php
luôn nhé)
Thêm code này vào đầu file index.php rồi lưu lại là xong. Bạn nên thêm sau thẻ mở <?php
như hình phía dưới nhé.
include('whitespacefix.php');
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
Tham khảo: https://wejn.org/stuff/wejnswpwhitespacefix.php.html