[英]Default Parameter Value of Method/Function Not Set Properly Passed as add_action() Callback in WordPress
The following sample plugin lets you download a text file when the download
button is pressed in the admin panel. 通过以下示例插件,您可以在管理面板中按下
download
按钮时下载文本文件。 The problem is that I expected the text file name would be hello_world.txt
but it somehow becomes options-general.txt
. 问题是我希望文本文件名是
hello_world.txt
但是它以某种方式变成options-general.txt
。
If this line header('Content-Disposition: attachment; filename="' . $file_name . '"');
如果此行
header('Content-Disposition: attachment; filename="' . $file_name . '"');
is directly set the file name like header('Content-Disposition: attachment; filename="hello_world.txt"');
直接设置文件名,例如
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.
感谢您的信息。
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. 据我所知,
init
钩子没有。 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.
在您的情况下,这意味着它实际上将覆盖默认值,而没有文件名,这反过来又导致浏览器使用提交表单的页面的文件名-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
. 我会将文件名作为表单中的隐藏字段传递并以
$_POST
形式发送,并以设置$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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.