wordpress plugin : settings api with OOP
I am now using OOP to create my wordpress plugin, however i am having a problem with the Settings API,i think the third parameter (the callback function) should ECHO/PRINT a returned value from the defined function, and not make the function Echo the input, look at this for more informations :
and this is the code snippet you need to look at :
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] .'" />';
bre开发者_JS百科ak;
}
}
because of the echo in the get_input function, the fields get printed above everything in every page, but if the wordpress function add_settings_field printed a returned value and not counted on a function to print it for it self, it would've worked,so how to out come this ?
Hope i got my idea clear, Best Regards
add_settings_field
expects it's third argument to be a callback, but you are actually calling your get_input
method. In order to use the get_input
method as a callback you need to pass an array containing the object and method name array($this, 'get_input')
.
You can't pass arguments to directly to the method when doing this, but I think if you pass an array of values as the sixth argument to your add_settings_field
call Wordpress will in turn pass this array to your callback when it's called. So, your add_settings_field
call would look something like this:
add_settings_field(
'test',
'test',
array($this, 'get_input'),
'test_settings',
'test_settings',
array(
'id' => 'test',
'name' => 'test',
'type' => 'text'
)
);
You'd then need to modify your callback function to extract the values from the array, something like the below.
public function get_input($args) {
$size = 40;
$droparray = NULL;
extract($args);
…
The value taken as a return is fed into a variable that has all of the page content. This page content is echoed out of php last and because of this anything that is echoed out will appear above it or below all of the content if the last thing you did was echo it.
You are getting the results the way they are due to the fact that you are not successfully passing a callback to the WordPress function. It is trying to resolve the '$this->get_input()' to pass the results to 'add_settings_field'.
In order to pass a class member function as a callback you need to pass an array with $this as reference and the function name as a string.
add_settings_field('test', 'test', $this->get_input('test', 'test', 'text'), 'test_settings', 'test_settings');
would become
add_settings_field('test', 'test', array(&$this, 'get_input'), 'test_settings', 'test_settings');
You can't pass callback arguments directly when referencing a class member function.
精彩评论