acf_field_checkbox

The Advanced Custom Fields acf field checkbox class.

Defined (1)

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

/core/fields/checkbox.php  
  1. class acf_field_checkbox extends acf_field 
  2.  
  3. /** 
  4. * __construct 
  5. * Set name / label needed for actions / filters 
  6. * @since 3.6 
  7. * @date 23/01/13 
  8. */ 
  9.  
  10. function __construct() 
  11. // vars 
  12. $this->name = 'checkbox'; 
  13. $this->label = __("Checkbox", 'acf'); 
  14. $this->category = __("Choice", 'acf'); 
  15. $this->defaults = array( 
  16. 'layout' => 'vertical',  
  17. 'choices' => array(),  
  18. 'default_value' => '',  
  19. ); 
  20.  
  21.  
  22. // do not delete! 
  23. parent::__construct(); 
  24.  
  25.  
  26. /** 
  27. * create_field() 
  28. * Create the HTML interface for your field 
  29. * @param $field - an array holding all the field's data 
  30. * @type action 
  31. * @since 3.6 
  32. * @date 23/01/13 
  33. */ 
  34.  
  35. function create_field( $field ) 
  36. // value must be array 
  37. if( !is_array($field['value']) ) 
  38. // perhaps this is a default value with new lines in it? 
  39. if( strpos($field['value'], "\n") !== false ) 
  40. // found multiple lines, explode it 
  41. $field['value'] = explode("\n", $field['value']); 
  42. else 
  43. $field['value'] = array( $field['value'] ); 
  44.  
  45.  
  46. // trim value 
  47. $field['value'] = array_map('trim', $field['value']); 
  48.  
  49.  
  50. // vars 
  51. $i = 0; 
  52. $e = '<input type="hidden" name="' . esc_attr($field['name']) . '" value="" />'; 
  53. $e .= '<ul class="acf-checkbox-list ' . esc_attr($field['class']) . ' ' . esc_attr($field['layout']) . '">'; 
  54.  
  55.  
  56. // checkbox saves an array 
  57. $field['name'] .= '[]'; 
  58.  
  59.  
  60. // foreach choices 
  61. foreach( $field['choices'] as $key => $value ) 
  62. // vars 
  63. $i++; 
  64. $atts = ''; 
  65.  
  66.  
  67. if( in_array($key, $field['value']) ) 
  68. $atts = 'checked="yes"'; 
  69. if( isset($field['disabled']) && in_array($key, $field['disabled']) ) 
  70. $atts .= ' disabled="true"'; 
  71.  
  72.  
  73. // each checkbox ID is generated with the $key, however, the first checkbox must not use $key so that it matches the field's label for attribute 
  74. $id = $field['id']; 
  75.  
  76. if( $i > 1 ) 
  77. $id .= '-' . $key; 
  78.  
  79. $e .= '<li><label><input id="' . esc_attr($id) . '" type="checkbox" class="' . esc_attr($field['class']) . '" name="' . esc_attr($field['name']) . '" value="' . esc_attr($key) . '" ' . $atts . ' />' . $value . '</label></li>'; 
  80.  
  81. $e .= '</ul>'; 
  82.  
  83.  
  84. // return 
  85. echo $e; 
  86.  
  87.  
  88. /** 
  89. * create_options() 
  90. * Create extra options for your field. This is rendered when editing a field. 
  91. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  92. * @type action 
  93. * @since 3.6 
  94. * @date 23/01/13 
  95. * @param $field - an array holding all the field's data 
  96. */ 
  97.  
  98. function create_options( $field ) 
  99. // vars 
  100. $key = $field['name']; 
  101.  
  102.  
  103. // implode checkboxes so they work in a textarea 
  104. if( is_array($field['choices']) ) 
  105. {  
  106. foreach( $field['choices'] as $k => $v ) 
  107. $field['choices'][ $k ] = $k . ' : ' . $v; 
  108. $field['choices'] = implode("\n", $field['choices']); 
  109.  
  110. ?> 
  111. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  112. <td class="label"> 
  113. <label for=""><?php _e("Choices", 'acf'); ?></label> 
  114. <p><?php _e("Enter each choice on a new line.", 'acf'); ?></p> 
  115. <p><?php _e("For more control, you may specify both a value and label like this:", 'acf'); ?></p> 
  116. <p><?php _e("red : Red", 'acf'); ?><br /><?php _e("blue : Blue", 'acf'); ?></p> 
  117. </td> 
  118. <td> 
  119. <?php 
  120.  
  121. do_action('acf/create_field', array( 
  122. 'type' => 'textarea',  
  123. 'class' => 'textarea field_option-choices',  
  124. 'name' => 'fields['.$key.'][choices]',  
  125. 'value' => $field['choices'],  
  126. )); 
  127.  
  128. ?> 
  129. </td> 
  130. </tr> 
  131. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  132. <td class="label"> 
  133. <label><?php _e("Default Value", 'acf'); ?></label> 
  134. <p class="description"><?php _e("Enter each default value on a new line", 'acf'); ?></p> 
  135. </td> 
  136. <td> 
  137. <?php 
  138.  
  139. do_action('acf/create_field', array( 
  140. 'type' => 'textarea',  
  141. 'name' => 'fields['.$key.'][default_value]',  
  142. 'value' => $field['default_value'],  
  143. )); 
  144.  
  145. ?> 
  146. </td> 
  147. </tr> 
  148. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  149. <td class="label"> 
  150. <label for=""><?php _e("Layout", 'acf'); ?></label> 
  151. </td> 
  152. <td> 
  153. <?php 
  154.  
  155. do_action('acf/create_field', array( 
  156. 'type' => 'radio',  
  157. 'name' => 'fields['.$key.'][layout]',  
  158. 'value' => $field['layout'],  
  159. 'layout' => 'horizontal',  
  160. 'choices' => array( 
  161. 'vertical' => __("Vertical", 'acf'),  
  162. 'horizontal' => __("Horizontal", 'acf') 
  163. )); 
  164.  
  165. ?> 
  166. </td> 
  167. </tr> 
  168. <?php 
  169.  
  170.