CSV导出插件中的邮件头已发送错误

时间:2013-04-11 作者:sta777

我构建了一个Wordpress插件,允许用户将所有页面及其级别和URL导出到csv文件。

我遇到的问题是,我在尝试导出csv文件时不断收到错误消息:

警告:无法修改标题信息-标题已发送

我已检查配置(&A);函数文件的空间,以及尝试不同的安装,但仍然无法得到这项工作!!!

请问有人知道怎么修理这个吗?

插件代码:

/*
Plugin Name: Your Site\'s Functionality Plugin
Description: All of the important functionality of your site belongs in this.
Version: 0.1
License: GPL
Author: Your Name
Author URI: yoururl
*/
add_action(\'admin_menu\', \'my_page_export_menu\');

function my_page_export_menu() {
   add_submenu_page(\'edit-pages.php\', __("Wordpress Export Pages to CSV", "my-page-export"), __("Export Page List", "my_page_export_menu"), \'edit_pages\', __FILE__,\'my_page_export_menu_options\');
}

function my_page_export_menu_options() {

echo \'<div id="treecontrol" style="margin:20px 0;position:relative;">
    <form method="post" id="download_form" action=""><input type="submit" name="download_csv" class="button-primary" value="Export" /></form>
</div>\';

if ( isset($_POST[\'download_csv\']) ) {

    $text = wp_list_pages("echo=0&title_li=");
    $html = preg_replace(\'/class=".*?"/\', \'\', $text);
    $html = strip_tags($html, \'<ul>, <li>, <a>\');
    $html = \'<ul>\'.$html.\'</ul>\';

    function get_depth(DOMElement $element)
    {
        $depth = -1;
        while (
            $element->parentNode->tagName === \'li\' || 
            $element->parentNode->tagName === \'ul\'
        ) {
            if ($element->parentNode->tagName === \'ul\') {
                $depth++;
            }
            $element = $element->parentNode;
        }
        return $depth;
    }

    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = false;
    $dom->loadHTML($html);
    echo \'<pre>\';
    $fp = fopen("php://output", "w");

    $file = \'test_export\';
    $filename = $file."_".date("Y-m-d_H-i",time());
    header("Content-type: text/csv");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header( "Content-disposition: filename=".$filename.".csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    foreach ($dom->getElementsByTagName(\'a\') as $li) {
     printf(
          \'%s%s%s\', 
          str_repeat(\',\', get_depth($li)),
          trim($li->childNodes->item(0)->nodeValue),
          \',,,,=HYPERLINK("\'.$li->getAttribute( \'href\' ).\'")\'.PHP_EOL
      );
    }           
    echo \'</pre>\';

} else {
    echo \'<ul>\';
    wp_list_pages("&title_li=");
    echo \'</ul>\';
}
}

2 个回复
最合适的回答,由SO网友:Rajeev Vyas 整理而成

将下载/输出代码移动到admin\\u init hook中。在输出任何内容之前调用此挂钩。它应该会起作用。

<?php 
/*
Plugin Name: Your Site\'s Functionality Plugin
Description: All of the important functionality of your site belongs in this.
Version: 0.1
License: GPL
Author: Your Name
Author URI: yoururl
*/
add_action(\'admin_menu\', \'my_page_export_menu\');

add_action(\'admin_init\',\'wpse9876_download_csv\');
function my_page_export_menu() {
   add_submenu_page(\'edit-pages.php\', __("Wordpress Export Pages to CSV", "my-page-export"), __("Export Page List", "my_page_export_menu"), \'edit_pages\', __FILE__,\'my_page_export_menu_options\');
}


function wpse9876_download_csv(){

    if ( isset($_POST[\'download_csv\']) ) {

        $text = wp_list_pages("echo=0&title_li=");
        $html = preg_replace(\'/class=".*?"/\', \'\', $text);
        $html = strip_tags($html, \'<ul>, <li>, <a>\');
        $html = \'<ul>\'.$html.\'</ul>\';

        function get_depth(DOMElement $element)
        {
            $depth = -1;
            while (
                    $element->parentNode->tagName === \'li\' ||
                    $element->parentNode->tagName === \'ul\'
            ) {
                if ($element->parentNode->tagName === \'ul\') {
                    $depth++;
                }
                $element = $element->parentNode;
            }
            return $depth;
        }

        $dom = new DOMDocument;
        $dom->preserveWhiteSpace = false;
        $dom->loadHTML($html);
        echo \'<pre>\';
        $fp = fopen("php://output", "w");

        $file = \'test_export\';
        $filename = $file."_".date("Y-m-d_H-i",time());
        header("Content-type: text/csv");
        header("Content-disposition: csv" . date("Y-m-d") . ".csv");
        header( "Content-disposition: filename=".$filename.".csv");
        header("Pragma: no-cache");
        header("Expires: 0");

        foreach ($dom->getElementsByTagName(\'a\') as $li) {
            printf(
                    \'%s%s%s\',
                    str_repeat(\',\', get_depth($li)),
                    trim($li->childNodes->item(0)->nodeValue),
                    \',,,,=HYPERLINK("\'.$li->getAttribute( \'href\' ).\'")\'.PHP_EOL
            );
        }
        echo \'</pre>\';
        exit;
    }
}

function my_page_export_menu_options() {

echo \'<div id="treecontrol" style="margin:20px 0;position:relative;">
    <form method="post" id="download_form" action=""><input type="submit" name="download_csv" class="button-primary" value="Export" /></form>
</div>\';

echo \'<ul>\';
wp_list_pages("&title_li=");
echo \'</ul>\';

}

SO网友:vancoder

你有

echo \'<pre>\';
在设置标题之前。这将打破它。

此外,您没有显示整个“无法修改…”错误消息。它应该包括导致问题的确切代码行的方向。

结束

相关推荐

当尝试向我的登录页面添加一个css文件时,会出现“Headers Always Sent”?

我看过的每一篇关于这个主题的教程都已经过时了,所以我希望这里的人能帮助我。我想为我的wp登录设置样式。带有CSS文件的php页面。但我想以一种在Wordpress更新时不会被覆盖的方式来做。据我所知,根据我读到的图茨,在我的主题函数中使用函数添加它的最佳方式。php文件。不幸的是,人们建议使用的代码并不适合我。这是我添加的代码。<?php function custom_login() { echo \'<link rel=\"stylesheet\" href=\"wplog