douzhan8303 2015-08-17 03:12
浏览 62
已采纳

挂钩到send_headers时WordPress发送404

I'm trying to hook into the send_headers function of WordPress so I can check to see if a sitemap.xml was requested. That way, I can serve up a custom PHP function to automatically generate the XML based on WordPress posts and pages.

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;

Everything is serving up properly, and my PHP file is including the appropriate headers:

<?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>

Why would WordPress be sending 404?

I tried using WordPress templates and that didn't seem to work.

$template = locate_template('sitemap.php');
load_template($template);

Is there another function I should be hooking into? Did I miss something else that I should be doing?

  • 写回答

1条回答 默认 最新

  • doutanggun9816 2015-08-17 03:23
    关注

    You may still need to send a 200 OK header since you're circumventing the normal approach to sending content.

    Add header("HTTP/1.1 200 OK"); before your include('sitemap.php');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作