Skip to content
Browse files

All field types that take an option array have been simplified to tak…

…e `key => value` pairs. Also updated readme and example-functions.php
  • Loading branch information...
1 parent 00be467 commit fb4d03d64ea5986a55d52a8b470961b84598ce69 @jtsternberg jtsternberg committed
Showing with 28 additions and 13 deletions.
  1. +9 −9 example-functions.php
  2. +15 −4 helpers/cmb_Meta_Box_types.php
  3. +4 −0 readme.md
View
18 example-functions.php
@@ -153,9 +153,9 @@ function cmb_sample_metaboxes( array $meta_boxes ) {
'id' => $prefix . 'test_select',
'type' => 'select',
'options' => array(
- array( 'name' => __( 'Option One', 'cmb' ), 'value' => 'standard', ),
- array( 'name' => __( 'Option Two', 'cmb' ), 'value' => 'custom', ),
- array( 'name' => __( 'Option Three', 'cmb' ), 'value' => 'none', ),
+ 'standard' => __( 'Option One', 'cmb' ),
+ 'custom' => __( 'Option Two', 'cmb' ),
+ 'none' => __( 'Option Three', 'cmb' ),
),
),
array(
@@ -164,9 +164,9 @@ function cmb_sample_metaboxes( array $meta_boxes ) {
'id' => $prefix . 'test_radio_inline',
'type' => 'radio_inline',
'options' => array(
- array( 'name' => __( 'Option One', 'cmb' ), 'value' => 'standard', ),
- array( 'name' => __( 'Option Two', 'cmb' ), 'value' => 'custom', ),
- array( 'name' => __( 'Option Three', 'cmb' ), 'value' => 'none', ),
+ 'standard' => __( 'Option One', 'cmb' ),
+ 'custom' => __( 'Option Two', 'cmb' ),
+ 'none' => __( 'Option Three', 'cmb' ),
),
),
array(
@@ -175,9 +175,9 @@ function cmb_sample_metaboxes( array $meta_boxes ) {
'id' => $prefix . 'test_radio',
'type' => 'radio',
'options' => array(
- array( 'name' => __( 'Option One', 'cmb' ), 'value' => 'standard', ),
- array( 'name' => __( 'Option Two', 'cmb' ), 'value' => 'custom', ),
- array( 'name' => __( 'Option Three', 'cmb' ), 'value' => 'none', ),
+ 'option1' => __( 'Option One', 'cmb' ),
+ 'option2' => __( 'Option Two', 'cmb' ),
+ 'option3' => __( 'Option Three', 'cmb' ),
),
),
array(
View
19 helpers/cmb_Meta_Box_types.php
@@ -367,8 +367,14 @@ public static function textarea_code( $field, $meta ) {
public static function select( $field, $meta ) {
$meta = self::esc( $meta );
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
- foreach ($field['options'] as $option) {
- echo '<option value="', $option['value'], '"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
+ foreach ( $field['options'] as $option_key => $option ) {
+
+ // Check for the "old" way
+ $label = isset( $option['name'] ) ? $option['name'] : $option;
+ $value = isset( $option['value'] ) ? $option['value'] : $option_key;
+
+ echo '<option value="', $value, '" ', selected( $meta == $value ) ,'>', $label, '</option>';
+
}
echo '</select>', self::desc( true );
}
@@ -377,8 +383,13 @@ public static function radio( $field, $meta ) {
$meta = self::esc( $meta );
echo '<ul>';
$i = 1;
- foreach ($field['options'] as $option) {
- echo '<li class="cmb_option"><input type="radio" name="', $field['id'], '" id="', $field['id'], $i,'" value="', $option['value'], '" ', checked( $meta == $option['value'] ), ' /> <label for="', $field['id'], $i, '">', $option['name'].'</label></li>';
+ foreach ( $field['options'] as $option_key => $option ) {
+
+ // Check for the "old" way
+ $label = isset( $option['name'] ) ? $option['name'] : $option;
+ $value = isset( $option['value'] ) ? $option['value'] : $option_key;
+
+ echo '<li class="cmb_option"><input type="radio" name="', $field['id'], '" id="', $field['id'], $i,'" value="', $value, '" ', checked( $meta == $value ), ' /> <label for="', $field['id'], $i, '">', $label ,'</label></li>';
$i++;
}
echo '</ul>', self::desc( true );
View
4 readme.md
@@ -86,6 +86,10 @@ This script is easy to install. If you can't figure it out you probably shouldn'
### 1.0.2
+**Enhancements**
+
+* All field types that take an option array have been simplified to take `key => value` pairs (vs `array( 'name' => 'value', 'value' => 'key', )`). This effects the 'select', 'radio', 'radio_inline' field types. The 'multicheck' field type was already using the `key => value` format. Backwards compatibility has been maintained for those using the older style.
+
### 1.0.1
**Bug Fixes**

0 comments on commit fb4d03d

Please sign in to comment.
Something went wrong with that request. Please try again.