Skip to content

Instantly share code, notes, and snippets.

@DarrylErentzen
Created February 28, 2014 19:29
Show Gist options
  • Save DarrylErentzen/9277980 to your computer and use it in GitHub Desktop.
Save DarrylErentzen/9277980 to your computer and use it in GitHub Desktop.
createFormControl
/**
* Helper-function outputs the correct form element (input tag, select tag, checkbox tag) for the given item
* @param $aOptionKey string name of the option (un-prefixed)
* @param $aOptionMeta mixed meta-data for $aOptionKey (either a string display-name or an array(display-name, option1, option2, ...)
* @param $savedOptionValue string current value for $aOptionKey
* @return void
*/
protected function createFormControl($aOptionKey, $aOptionMeta, $savedOptionValue, $aOptionFormat = 'select') {
if (is_array($aOptionMeta) && count($aOptionMeta) >= 2) {
switch($aOptionFormat) {
case 'select':
// Drop-down list
$choices = array_slice($aOptionMeta, 1);
?>
<p><select name="<?php echo $aOptionKey ?>" id="<?php echo $aOptionKey ?>">
<?php
foreach ($choices as $aChoice) {
$selected = ($aChoice == $savedOptionValue) ? 'selected="selected"' : '';
?>
<option value="<?php echo $aChoice ?>" <?php echo $selected ?>><?php echo $this->getOptionValueI18nString($aChoice) ?></option>
<?php
}
?>
</select></p>
<?php
break;
case 'checkbox':
$choices = array_slice($aOptionMeta, 1);
?>
<div>
<?php
foreach ($choices as $aChoice) {
$checked = (in_array($aChoice,$savedOptionValue)) ? 'checked="checked"' : '';
?>
<div style="display:inline-block; min-width:150px;"><input type="checkbox" name="<?php echo $aOptionKey ?>[]" value="<?php echo $aChoice ?>" <?php echo $checked ?>><?php echo $this->getOptionValueI18nString($aChoice) ?></div>
<?php
}print_r($savedOptionValue);
?>
</div>
<?php
break;
}
}
else { // Simple input field
?>
<p><input type="text" name="<?php echo $aOptionKey ?>" id="<?php echo $aOptionKey ?>"
value="<?php echo esc_attr($savedOptionValue) ?>" size="50"/></p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment