/** * Adds a custom field type for select multiples. * @param object $field The CMB2_Field type object. * @param string $value The saved (and escaped) value. * @param int $object_id The current post ID. * @param string $object_type The current object type. * @param object $field_type_object The CMB2_Types object. * @return void */ function cmb2_render_select_multiple_field_type($field, $escaped_value, $object_id, $object_type, $field_type_object) { $select_multiple = '<select class="widefat" multiple name="' . $field->args['_name'] . '[]" id="' . $field->args['_id'] . '"'; foreach ($field->args['attributes'] as $attribute => $value) { $select_multiple .= " {$attribute}=\"{$value}\""; } $select_multiple .= ' />'; foreach ($field->args['options'] as $value => $name) { $selected = $escaped_value && in_array($value, $escaped_value) ? 'selected="selected"' : ''; $select_multiple .= '<option class="cmb2-option" value="' . esc_attr($value) . '" ' . $selected . '>' . esc_html($name) . '</option>'; } $select_multiple .= '</select>'; $select_multiple .= $field_type_object->_desc(true); echo $select_multiple; // WPCS: XSS ok. }
/** * Render 'star rating' custom field type * * @since 0.1.0 * * @param array $field The passed in `CMB2_Field` object * @param mixed $value The value of this field escaped. * It defaults to `sanitize_text_field`. * If you need the unescaped value, you can access it * via `$field->value()` * @param int $object_id The ID of the current object * @param string $object_type The type of object you are working with. * Most commonly, `post` (this applies to all post-types), * but could also be `comment`, `user` or `options-page`. * @param object $field_type_object The `CMB2_Types` object */ function eh_cmb2_render_star_rating_field_callback($field, $value, $object_id, $object_type, $field_type_object) { // enqueue styles wp_enqueue_style('star-rating-metabox-css', plugin_dir_url(__FILE__) . '/css/star-rating-field-type.css', array('cmb2-styles'), 'all', false); ?> <section id="cmb2-star-rating-metabox"> <fieldset> <span class="star-cb-group"> <?php $y = 5; while ($y > 0) { ?> <input type="radio" id="rating-<?php echo $y; ?> " name="<?php echo $field_type_object->_id(false); ?> " value="<?php echo $y; ?> " <?php checked($value, $y); ?> /> <label for="rating-<?php echo $y; ?> "><?php echo $y; ?> </label> <?php $y--; } ?> </span> </fieldset> </section> <?php echo $field_type_object->_desc(true); }
/** * Render 'address' custom field type * * @since 0.1.0 * * @param array $field The passed in `CMB2_Field` object * @param mixed $value The value of this field escaped. * It defaults to `sanitize_text_field`. * If you need the unescaped value, you can access it * via `$field->value()` * @param int $object_id The ID of the current object * @param string $object_type The type of object you are working with. * Most commonly, `post` (this applies to all post-types), * but could also be `comment`, `user` or `options-page`. * @param object $field_type_object The `CMB2_Types` object */ function jt_cmb2_render_address_field_callback($field, $value, $object_id, $object_type, $field_type_object) { // can override via the field options param $select_text = esc_html($field_type_object->_text('address_select_state_text', 'Select a State')); $state_list = array('' => $select_text, 'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'DC' => 'District Of Columbia', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming'); // make sure we specify each part of the value we need. $value = wp_parse_args($value, array('address-1' => '', 'address-2' => '', 'city' => '', 'state' => '', 'zip' => '')); $state_options = ''; foreach ($state_list as $abrev => $state) { $state_options .= '<option value="' . $abrev . '" ' . selected($value['state'], $abrev, false) . '>' . $state . '</option>'; } ?> <div><p><label for="<?php echo $field_type_object->_id('_address_1'); ?> "><?php echo esc_html($field_type_object->_text('address_address_1_text', 'Address 1')); ?> </label></p> <?php echo $field_type_object->input(array('name' => $field_type_object->_name('[address-1]'), 'id' => $field_type_object->_id('_address_1'), 'value' => $value['address-1'])); ?> </div> <div><p><label for="<?php echo $field_type_object->_id('_address_2'); ?> '"><?php echo esc_html($field_type_object->_text('address_address_2_text', 'Address 2')); ?> </label></p> <?php echo $field_type_object->input(array('name' => $field_type_object->_name('[address-2]'), 'id' => $field_type_object->_id('_address_2'), 'value' => $value['address-2'])); ?> </div> <div class="alignleft"><p><label for="<?php echo $field_type_object->_id('_city'); ?> '"><?php echo esc_html($field_type_object->_text('address_city_text', 'City')); ?> </label></p> <?php echo $field_type_object->input(array('class' => 'cmb_text_small', 'name' => $field_type_object->_name('[city]'), 'id' => $field_type_object->_id('_city'), 'value' => $value['city'])); ?> </div> <div class="alignleft"><p><label for="<?php echo $field_type_object->_id('_state'); ?> '"><?php echo esc_html($field_type_object->_text('address_state_text', 'State')); ?> </label></p> <?php echo $field_type_object->select(array('name' => $field_type_object->_name('[state]'), 'id' => $field_type_object->_id('_state'), 'options' => $state_options)); ?> </div> <div class="alignleft"><p><label for="<?php echo $field_type_object->_id('_zip'); ?> '"><?php echo esc_html($field_type_object->_text('address_zip_text', 'Zip')); ?> </label></p> <?php echo $field_type_object->input(array('class' => 'cmb_text_small', 'name' => $field_type_object->_name('[zip]'), 'id' => $field_type_object->_id('_zip'), 'value' => $value['zip'], 'type' => 'number')); ?> </div> <?php echo $field_type_object->_desc(true); }