我现在使用OOP来创建我的wordpress插件,但是我在使用Settings API时遇到了问题,我认为第三个参数(回调函数)应该回显/打印已定义函数的返回值,而不是让函数Echo作为输入,更多信息请看下面的内容:

下面是您需要查看的代码片段:
public function another_function(){
add_settings_field('test', 'test', $this->get_input('test', 'test', 'text'), 'test_settings', 'test_settings');
}
public function get_input($id, $name, $type, $size=40, $droparray = NULL){
$options = get_option('brutal_settings_group');
switch ($type) {
case 'text':
echo '<input id="'. $id .'" name="test['. $name .']" size="'. $size .'" type="text" value="'. $options[$name] .'" />';
break;
}
}由于wordpress函数中的回显,字段打印在每一页的所有内容之上,但是如果get_input函数add_settings_field打印了一个返回值,并且不指望函数自己打印返回值,那么它应该是有效的,那么如何输出呢?
希望我的想法清楚了,致以最好的问候
发布于 2011-08-19 08:30:17
add_settings_field期望它的第三个参数是一个callback,但实际上你调用的是你的get_input方法。为了使用get_input方法作为回调,您需要传递一个包含对象和方法名array($this, 'get_input')的数组。
在这样做的时候,你不能直接将参数传递给方法,但我认为如果你将一个值数组作为第六个参数传递给你的add_settings_field调用,当它被调用时,Wordpress会反过来将这个数组传递给你的回调。因此,您的add_settings_field调用将如下所示:
add_settings_field(
'test',
'test',
array($this, 'get_input'),
'test_settings',
'test_settings',
array(
'id' => 'test',
'name' => 'test',
'type' => 'text'
)
);然后,您需要修改回调函数以从数组中提取值,如下所示。
public function get_input($args) {
$size = 40;
$droparray = NULL;
extract($args);
…发布于 2011-08-17 00:04:18
作为返回值的值被提供给一个包含所有页面内容的变量。这个页面的内容是最后从php回显出来的,正因为如此,如果你做的最后一件事是回显它,那么任何回显出来的内容都会出现在它的上方或下方。
发布于 2011-08-20 17:37:08
由于您没有成功地将回调传递给WordPress函数,所以您会得到结果的原样。它正在尝试解析'$this->get_input()‘以将结果传递给'add_settings_field’。
为了将类成员函数作为回调传递,您需要传递一个数组,该数组以$this作为引用,函数名作为字符串。
add_settings_field('test', 'test', $this->get_input('test', 'test', 'text'), 'test_settings', 'test_settings');会变成
add_settings_field('test', 'test', array(&$this, 'get_input'), 'test_settings', 'test_settings');引用类成员函数时,不能直接传递回调参数。
https://stackoverflow.com/questions/7081205
复制相似问题