acf_field_radio

The Advanced Custom Fields acf field radio class.

Defined (1)

The class is defined in the following location(s).

/core/fields/radio.php  
  1. class acf_field_radio extends acf_field 
  2. /** 
  3. * __construct 
  4. * Set name / label needed for actions / filters 
  5. * @since 3.6 
  6. * @date 23/01/13 
  7. */ 
  8.  
  9. function __construct() 
  10. // vars 
  11. $this->name = 'radio'; 
  12. $this->label = __("Radio Button", 'acf'); 
  13. $this->category = __("Choice", 'acf'); 
  14. $this->defaults = array( 
  15. 'layout' => 'vertical',  
  16. 'choices' => array(),  
  17. 'default_value' => '',  
  18. 'other_choice' => 0,  
  19. 'save_other_choice' => 0,  
  20. ); 
  21.  
  22.  
  23. // do not delete! 
  24. parent::__construct(); 
  25.  
  26.  
  27.  
  28. /** 
  29. * create_field() 
  30. * Create the HTML interface for your field 
  31. * @param $field - an array holding all the field's data 
  32. * @type action 
  33. * @since 3.6 
  34. * @date 23/01/13 
  35. */ 
  36.  
  37. function create_field( $field ) 
  38. // vars 
  39. $i = 0; 
  40. $e = '<ul class="acf-radio-list ' . esc_attr($field['class']) . ' ' . esc_attr($field['layout']) . '">'; 
  41.  
  42.  
  43. // add choices 
  44. if( is_array($field['choices']) ) 
  45. foreach( $field['choices'] as $key => $value ) 
  46. // vars 
  47. $i++; 
  48. $atts = ''; 
  49.  
  50.  
  51. // if there is no value and this is the first of the choices, select this on by default 
  52. if( $field['value'] === false ) 
  53. if( $i === 1 ) 
  54. $atts = 'checked="checked" data-checked="checked"'; 
  55. else 
  56. if( strval($key) === strval($field['value']) ) 
  57. $atts = 'checked="checked" data-checked="checked"'; 
  58.  
  59.  
  60. // HTML 
  61. $e .= '<li><label><input id="' . esc_attr($field['id']) . '-' . esc_attr($key) . '" type="radio" name="' . esc_attr($field['name']) . '" value="' . esc_attr($key) . '" ' . esc_attr( $atts ) . ' />' . $value . '</label></li>'; 
  62.  
  63.  
  64. // other choice 
  65. if( $field['other_choice'] ) 
  66. // vars 
  67. $atts = ''; 
  68. $atts2 = 'name="" value="" style="display:none"'; 
  69.  
  70.  
  71. if( $field['value'] !== false ) 
  72. if( !isset($field['choices'][ $field['value'] ]) ) 
  73. $atts = 'checked="checked" data-checked="checked"'; 
  74. $atts2 = 'name="' . esc_attr($field['name']) . '" value="' . esc_attr($field['value']) . '"' ; 
  75.  
  76.  
  77. $e .= '<li><label><input id="' . esc_attr($field['id']) . '-other" type="radio" name="' . esc_attr($field['name']) . '" value="other" ' . $atts . ' />' . __("Other", 'acf') . '</label> <input type="text" ' . $atts2 . ' /></li>'; 
  78.  
  79.  
  80. $e .= '</ul>'; 
  81.  
  82. echo $e; 
  83.  
  84.  
  85.  
  86. /** 
  87. * create_options() 
  88. * Create extra options for your field. This is rendered when editing a field. 
  89. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  90. * @type action 
  91. * @since 3.6 
  92. * @date 23/01/13 
  93. * @param $field - an array holding all the field's data 
  94. */ 
  95.  
  96. function create_options( $field ) 
  97. // vars 
  98. $key = $field['name']; 
  99.  
  100. // implode checkboxes so they work in a textarea 
  101. if( is_array($field['choices']) ) 
  102. {  
  103. foreach( $field['choices'] as $k => $v ) 
  104. $field['choices'][ $k ] = $k . ' : ' . $v; 
  105. $field['choices'] = implode("\n", $field['choices']); 
  106.  
  107. ?> 
  108. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  109. <td class="label"> 
  110. <label for=""><?php _e("Choices", 'acf'); ?></label> 
  111. <p class="description"><?php _e("Enter your choices one per line", 'acf'); ?><br /> 
  112. <br /> 
  113. <?php _e("Red", 'acf'); ?><br /> 
  114. <?php _e("Blue", 'acf'); ?><br /> 
  115. <br /> 
  116. <?php _e("red : Red", 'acf'); ?><br /> 
  117. <?php _e("blue : Blue", 'acf'); ?><br /> 
  118. </p> 
  119. </td> 
  120. <td> 
  121. <?php 
  122.  
  123. do_action('acf/create_field', array( 
  124. 'type' => 'textarea',  
  125. 'class' => 'textarea field_option-choices',  
  126. 'name' => 'fields['.$key.'][choices]',  
  127. 'value' => $field['choices'],  
  128. )); 
  129.  
  130. ?> 
  131. <div class="radio-option-other_choice"> 
  132. <?php 
  133.  
  134. do_action('acf/create_field', array( 
  135. 'type' => 'true_false',  
  136. 'name' => 'fields['.$key.'][other_choice]',  
  137. 'value' => $field['other_choice'],  
  138. 'message' => __("Add 'other' choice to allow for custom values", 'acf') 
  139. )); 
  140.  
  141. ?> 
  142. </div> 
  143. <div class="radio-option-save_other_choice" <?php if( !$field['other_choice'] ): ?>style="display:none"<?php endif; ?>> 
  144. <?php 
  145.  
  146. do_action('acf/create_field', array( 
  147. 'type' => 'true_false',  
  148. 'name' => 'fields['.$key.'][save_other_choice]',  
  149. 'value' => $field['save_other_choice'],  
  150. 'message' => __("Save 'other' values to the field's choices", 'acf') 
  151. )); 
  152.  
  153. ?> 
  154. </div> 
  155. </td> 
  156. </tr> 
  157. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  158. <td class="label"> 
  159. <label><?php _e("Default Value", 'acf'); ?></label> 
  160. </td> 
  161. <td> 
  162. <?php 
  163.  
  164. do_action('acf/create_field', array( 
  165. 'type' => 'text',  
  166. 'name' => 'fields['.$key.'][default_value]',  
  167. 'value' => $field['default_value'],  
  168. )); 
  169.  
  170. ?> 
  171. </td> 
  172. </tr> 
  173. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  174. <td class="label"> 
  175. <label for=""><?php _e("Layout", 'acf'); ?></label> 
  176. </td> 
  177. <td> 
  178. <?php 
  179.  
  180. do_action('acf/create_field', array( 
  181. 'type' => 'radio',  
  182. 'name' => 'fields['.$key.'][layout]',  
  183. 'value' => $field['layout'],  
  184. 'layout' => 'horizontal',  
  185. 'choices' => array( 
  186. 'vertical' => __("Vertical", 'acf'),  
  187. 'horizontal' => __("Horizontal", 'acf') 
  188. )); 
  189.  
  190. ?> 
  191. </td> 
  192. </tr> 
  193. <?php 
  194.  
  195.  
  196.  
  197. /** 
  198. * update_value() 
  199. * This filter is appied to the $value before it is updated in the db 
  200. * @type filter 
  201. * @since 3.6 
  202. * @date 23/01/13 
  203. * @param $value - the value which will be saved in the database 
  204. * @param $post_id - the $post_id of which the value will be saved 
  205. * @param $field - the field array holding all the field options 
  206. * @return $value - the modified value 
  207. */ 
  208.  
  209. function update_value( $value, $post_id, $field ) 
  210. // validate 
  211. if( $field['save_other_choice'] ) 
  212. // value isn't in choices yet 
  213. if( !isset($field['choices'][ $value ]) ) 
  214. // update $field 
  215. $field['choices'][ $value ] = $value; 
  216.  
  217.  
  218. // can save 
  219. if( isset($field['field_group']) ) 
  220. do_action('acf/update_field', $field, $field['field_group']); 
  221.  
  222. }  
  223.  
  224. return $value; 
  225.