WordPress-Simple-Fields
WordPress-Simple-Fields copied to clipboard
Last value of dropdown field duplicated
When adding a dropdown field using the PHP API the last value in the dropdown gets duplicated in the top.
With this code:
simple_fields_register_field_group('sections_group', array(
[...]
array(
'slug' => 'color',
'name' => __('Color', 'h5b'),
'type' => 'dropdown',
'options' => array(
'values' => array(
array(
'num' => 'red',
'value' => __('Red', 'h5b')
),
array(
'num' => 'green',
'value' => __('Green', 'h5b')
),
array(
'num' => 'blue',
'value' => __('Blue', 'h5b')
)
)
)
)
[...]
));
The generated dropdown looks like this:
<select [...]>
<option value="dropdown_num_0">Blue</option>
<option value="dropdown_num_red">Red</option>
<option value="dropdown_num_green">Green</option>
<option value="dropdown_num_blue">Blue</option>
</select>
As you can see the value is 0 even though the text is the same as the last option's. Perhaps I'm missing something (like perhaps you can pass in the first option's text somewhere to the function) but even still it's kind of weird it duplicates the last one.
Also, I was a bit surprised all of my values were prepended with "dropdown_num_" - is there any way to avoid this?
Cheers
I believe the "num" should be a number, so maybe that's what confusing the plugin. Try changing it to numbers and see if that helps.
The dropdop_num_
You are correct in that switching to numbers fixes the duplication problem (for some reason :P) but the weird thing is that nothing except the duplication issue prevents you from using text. I mean SimpleFields doesn't throw an error or output a faulty
Anyway, as you can see in in my example I want to store different colors for the user to select. These colors are later used as class-names on certain elements. With the ability to ONLY store numbers I would then have to map each color to a number is that the idea? IMO that's a bit of a hassle. Would be better if I could just store "red" as the option's value instead of "dropdown_num_0" and then having to translate "dropdown_num_0" to "red".
Instead of just going
<div class="<?php echo $value_of_dropdown ?>">...</div>
I now have to do something like:
<?php $converter = array('dropdown_num_0' => 'red', 'dropdown_num_1' => 'green') # etc ?>
<div class="<?php echo $converter[$value_of_dropdown] ?>">...</div>
Also, not sure I get the bit about functions still working after changing values. I mean, the value and the actual text are already separated so the same is true even if I stored "red" instead of "dropdown_num_0".
The "dropdown_num_0"-thing also makes comparisons a little harder, for example:
if ($options['color'] != 'dropdown_num_' and $options['color'] != 'dropdown_num_0') // make sure user has selected a color
Instead of just
if ($options['color'])
One last thing; it would be awesome if you could also set which option should be default. Something as simple as just passing in 'default' => true next to 'num' and 'value'.
I understand that changing the behaviour of 'num' is not possible due to backwards compatibility, but would you consider adding an alternative to 'num' (like 'key' for example) that, if used, is used as the option's EXACT value (without "dropdown_num_"). So you could go:
array(
'key' => '',
'value' => __('-- Choose Color ---', 'h5b')
),
array(
'key' => 'red',
'value' => __('Red', 'h5b')
),
array(
'key' => 'green',
'value' => __('Green', 'h5b')
),
array(
'key' => 'blue',
'value' => __('Blue', 'h5b'),
'default' => true
)
Which would then output:
<select [...]>
<option value="">--- Choose Color ---</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue" selected>Blue</option>
</select>
IMO it makes more sense to 1) allow any value for options and not just numbers (as HTML does) and 2) not change the name of the value (because that's honestly just confusing :P)