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

Nếu bạn thấy bài viết có ích hãy sao chép link và chia sẻ bài viết
Đã copy
daotiendung

Tiến Dũng Đào chuyên quản lý, vận hành các dịch vụ website. Anh có nhiều năm kinh nghiệm về VPS, Hosting, technical SEO, CMS. Đặc biệt yêu thích WordPress với hơn 5 năm phát triển theme và plugin. Sở thích của anh là đọc, viết blog, đi du lịch, tập võ và chia sẻ các kiến thức cho mọi người.

Bài viết liên quan