在我们的一个wordPress页面上,我们想通过浏览器下载文件.据我所知,这是通过修改标题来完成的,因此页面认为它是某种类型的文件(例如.txt).然而,这个主题,以及在这个页面加载之前加载了几十个插件,其中许多插件正在输出某种数据,或者以某种方式修改标题,我不断得到臭名昭着的标题错误:
警告:无法修改标题信息 - 已在/ home/keep/public_html/wp-content/themes /中发送的标题(输出从/home/keep/public_html/wp-content/themes/procyon/header.php:2开始)第97行的procyon/functions.php
那么,我怎么可能绕过其他插件在我想要之前修改标题信息的事实呢?肯定有一些方法,因为其他插件彼此没有这个问题.
对于那些喜欢看到某种努力的人,这里是我正在使用的代码:
$tmp_handle = fopen('php://memory', 'r+');
foreach ($arr as $fields) {
fputcsv($tmp_handle, $fields);
}
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
rewind($tmp_handle);
echo stream_get_contents($tmp_handle);
Run Code Online (Sandbox Code Playgroud)
我非常感谢你对此事的一些帮助,谢谢!
如果您只想让用户下载某些东西,那么您就不需要使用模板系统了.您确定在"template_redirect"之前没有输出任何内容,因此您可以使用此事实来编写如下内容:
add_action('template_redirect', 'download_page');
function download_page() {
if($_GET['download'] == 'true') {
//Output header information and the file content
exit; // Stops Wordpress from executing anything else.
}
}
Run Code Online (Sandbox Code Playgroud)
现在也许您确实需要模板系统来传递文件,我不知道,但您的描述没有具体说明.
| 归档时间: |
|
| 查看次数: |
3010 次 |
| 最近记录: |