Php WordPress连接到发送头时发送404

Php WordPress连接到发送头时发送404,php,wordpress,http-status-code-404,Php,Wordpress,Http Status Code 404,我正在尝试连接WordPress的send_headers功能,以便检查是否请求了sitemap.xml。这样,我就可以提供一个定制的PHP函数,根据WordPress帖子和页面自动生成XML add_action( 'send_headers', 'custom_send_headers'); if( !function_exists('custom_send_headers') ) : function custom_send_headers( ) { glo

我正在尝试连接WordPress的
send_headers
功能,以便检查是否请求了
sitemap.xml
。这样,我就可以提供一个定制的PHP函数,根据WordPress帖子和页面自动生成XML

add_action( 'send_headers', 'custom_send_headers');
if( !function_exists('custom_send_headers') ) :
    function custom_send_headers( )
    {
        global $route, $wp_query, $window_title;
        $bits = explode( "/", $_SERVER['REQUEST_URI'] );

        if( $bits[1] === 'sitemap.xml' )
        {
            if ( $wp_query->is_404 ) {
                $wp_query->is_404 = false;
            }
            include('sitemap.php');
            die();
        }
    }
endif;
一切正常,我的PHP文件包含适当的标题:

<?php header('Content-Type: application/xml'); ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    ....
</urlset>

有没有其他我应该参与的功能?我是否错过了我应该做的其他事情?

您可能仍然需要发送
200 OK
标题,因为您正在绕过发送内容的常规方法


添加
标题(“HTTP/1.1200ok”)
包含('sitemap.php')之前

您是否已为
sitemap.xml
注册了重写规则?您是否建议我手动修改.htaccess文件?我正试图避免这种情况,因此链接到WordPress的标题。您可能仍然需要发送
200OK
标题,因为您绕过了发送内容的常规方法。你能试试
标题(“HTTP/1.1200ok”)包含
之前,代码>起作用。谢谢如果你想提交答案,我会尽可能接受。感谢您的快速响应!谢谢我还询问了重写规则,以查看您编写的代码是否位于未找到的文件的上下文中。您可能可以放弃
$wp\u query->is\u 404=false太多内容或在其位置包含200 OK标题:)
$template = locate_template('sitemap.php');
load_template($template);