我正在尝试为我的wordpress插件提供一些报告.此报告必须可以作为excel下载.基本上我所做的就是创建一个按钮,当按下该按钮时,我将查询数据库并从结果中获得卓越的表现.
现在,我需要修改标题以将结果作为excel文件返回.这是我的方法:
header('Content-disposition: attachment; filename='.$filename.'.xls');
header('Content-type: application/force-download');
header('Content-transfer-encoding: binary');
header('Pragma: public');
print "\xEF\xBB\xBF"; // UTF-8 BOM
问题是它返回以下错误:
Warning: Cannot modify header @R_64_4045@ion - headers already sent by (output started at .....\wp-admin\menu-header.PHP:137)
为此,我可能需要ob_start(),但这不是必须将ob_start()放在第一个标头(来自wordpress的核心文件)之前.如果可能的话,我不希望修改核心文件.
还是wordpress自己的钩子“ send_headers”?但我无法使其正常工作,但仍会产生相同的错误.
那么,我需要解决什么问题呢?是否有另一种方法可以从wordpress插件生成Excel文件?
任何帮助表示赞赏!
解决方法:
您发送标题太晚了,因为header()已经在menu-header.PHP中发送了.从提供的代码中,我看不到您正在发送标头的位置,但是在plugins_loaded操作中将是一个不错的选择,因为在所有插件均已加载且发送任何输出之前,将调用该操作挂钩.
下载链接如下所示:
<a href="<?PHP echo admin_url( '?download' ); ?>">download</a>
并且,plugins_loaded操作:
add_action( 'plugins_loaded', function() {
if ( isset( $_GET['download'] ) ) {
// here you can create .xls file
header('Content-disposition: attachment; filename='.$filename.'.xls');
header('Content-type: application/force-download');
header('Content-transfer-encoding: binary');
header('Pragma: public');
die();
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。