Wordpress Settings API callbacks
Does Wordpress settings API have any way of passing custom arguments to the callba开发者_JS百科cks or something? So I don't have to define seventeen hundred callbacks?
Or knowing the field/section/group name so I can do for example a text input handler that does:
$options = get_option( $setting );
echo "<input id='$option_id' name='$setting[$field]' size='40'
type='text' value='{$options[$field]}' />";
instead of a different callback for each field with all those values hardcoded?
There's an optional parameter $args which carries an array of parameters. What I did was to set it to this:
array('option' => $this->optionsPrefix.'server'));
and then do this in the common calback
function display_text_option($args)
{
$args = array_merge(array(
'option' => '',
'pass' => false), $args);
$option = $args['option'];
$options = get_option(LDFOPTION);
if($options == null)
$options = array();
$options = array_merge(array(
$option => ''), $options);
$value = $options[$option];
$type = $args['pass']?'password':'text';
echo " <input id='$option', name='".LDFOPTION."[$option]' size='40' type='$type' value='$value' />";
}
I'll probably do some more refactoring later on as thoughts come up
精彩评论