我构建了一个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>\';
}
}
最合适的回答,由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>\';
}