以下示例插件可让您download
在管理面板中按下按钮时下载文本文件。问题是我预计文本文件名会是hello_world.txt
,但它以某种方式变为options-general.txt
.
如果这一行header('Content-Disposition: attachment; filename="' . $file_name . '"');
直接设置文件名就好header('Content-Disposition: attachment; filename="hello_world.txt"');
了。
/* Plugin Name: Sample Download Button */
$sample_download_button = new Sample_Download_Button_AdminPage('Sample Download Button');
add_action('init', array($sample_download_button, "download_text"));
add_action('admin_menu', array($sample_download_button , "admin_menu"));
class Sample_Download_Button_AdminPage {
function __construct($pagetitle, $menutitle='', $privilege='manage_options', $pageslug='') {
$this->pagetitle = $pagetitle;
$this->menutitle = !empty($menutitle) ? $menutitle : $pagetitle;
$this->privilege = $privilege;
$this->pageslug = !empty($pageslug) ? $pageslug : basename(__FILE__, ".php");
}
function admin_menu() {
add_options_page($this->pagetitle, $this->menutitle, $this->privilege, $this->pageslug, array(&$this, 'admin_page'));
}
function admin_page() {
?>
<div class="wrap">
<h1>Download Button Sample</h1>
<form action="" method="post" target="_blank">
<input type="submit" value="download" name="download">
</form>
</div>
<?php
}
function download_text($file_name='hello_world.txt') {
if (!isset($_POST['download'])) return;
$your_text = 'hello world';
header("Content-type: text/plain");
header('Content-Disposition: attachment; filename="' . $file_name . '"');
echo $your_text;
exit;
}
}
为什么?以及如何设置默认参数值?我用普通功能尝试过,它的默认值也没有反映。感谢您的信息。