Reputation: 2632
The following sample plugin lets you download a text file when the download
button is pressed in the admin panel. The problem is that I expected the text file name would be hello_world.txt
but it somehow becomes options-general.txt
.
If this line header('Content-Disposition: attachment; filename="' . $file_name . '"');
is directly set the file name like header('Content-Disposition: attachment; filename="hello_world.txt"');
it works fine.
/* 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;
}
}
Why is it? And how can I set a default parameter value? I tried it with a normal function and its default value is also not reflected. Thanks for your info.
Upvotes: 0
Views: 332
Reputation: 2850
I tested this a couple of ways, including copying your complete function into a 'sandbox' install I have.
Not all hooks pass parameters. From what I can tell, the init
hook does not. Whether it accepts/passes a parameter is determined when the hook is defined. Apparently, when a hook fires that wasn't intended to pass a parameter, that passes an empty string to the callback. In your case, this means that it is effectively overwriting your default and leaving you without a filename, which in turn causes the browser to use the file name of the page to which the form was submitted-- options-general.php.
I'd pass the file name as a hidden field in the form and send it in $_POST
, and set my default the way your have set $your_text
.
function download_text() {
if (!isset($_POST['download'])) return;
if (!isset($_POST['filename'])) $file_name = $_POST['filename'];
else $file_name = 'hello_world.txt';
$your_text = 'hello world';
header("Content-type: text/plain");
header('Content-Disposition: attachment; filename="' . $file_name . '"');
echo $your_text;
exit;
}
Upvotes: 1