acf_field_number

The Advanced Custom Fields acf field number class.

Defined (1)

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

/core/fields/number.php  
  1. class acf_field_number 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 = 'number'; 
  13. $this->label = __("Number", 'acf'); 
  14. $this->defaults = array( 
  15. 'default_value' => '',  
  16. 'min' => '',  
  17. 'max' => '',  
  18. 'step' => '',  
  19. 'placeholder' => '',  
  20. 'prepend' => '',  
  21. 'append' => '' 
  22. ); 
  23.  
  24.  
  25. // do not delete! 
  26. parent::__construct(); 
  27.  
  28.  
  29. /** 
  30. * create_field() 
  31. * Create the HTML interface for your field 
  32. * @param $field - an array holding all the field's data 
  33. * @type action 
  34. * @since 3.6 
  35. * @date 23/01/13 
  36. */ 
  37.  
  38. function create_field( $field ) 
  39. // vars 
  40. $o = array( 'id', 'class', 'min', 'max', 'step', 'name', 'value', 'placeholder' ); 
  41. $e = ''; 
  42.  
  43.  
  44. // step 
  45. if( !$field['step'] ) 
  46. $field['step'] = 'any'; 
  47.  
  48. // prepend 
  49. if( $field['prepend'] !== "" ) 
  50. $field['class'] .= ' acf-is-prepended'; 
  51. $e .= '<div class="acf-input-prepend">' . $field['prepend'] . '</div>'; 
  52.  
  53.  
  54. // append 
  55. if( $field['append'] !== "" ) 
  56. $field['class'] .= ' acf-is-appended'; 
  57. $e .= '<div class="acf-input-append">' . $field['append'] . '</div>'; 
  58.  
  59.  
  60. $e .= '<div class="acf-input-wrap">'; 
  61. $e .= '<input type="number"'; 
  62.  
  63. foreach( $o as $k ) 
  64. $e .= ' ' . $k . '="' . esc_attr( $field[ $k ] ) . '"';  
  65.  
  66. $e .= ' />'; 
  67. $e .= '</div>'; 
  68.  
  69.  
  70. // return 
  71. echo $e; 
  72.  
  73.  
  74.  
  75. /** 
  76. * create_options() 
  77. * Create extra options for your field. This is rendered when editing a field. 
  78. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  79. * @type action 
  80. * @since 3.6 
  81. * @date 23/01/13 
  82. * @param $field - an array holding all the field's data 
  83. */ 
  84.  
  85. function create_options( $field ) 
  86. // vars 
  87. $key = $field['name']; 
  88.  
  89. ?> 
  90. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  91. <td class="label"> 
  92. <label><?php _e("Default Value", 'acf'); ?></label> 
  93. <p><?php _e("Appears when creating a new post", 'acf') ?></p> 
  94. </td> 
  95. <td> 
  96. <?php 
  97.  
  98. do_action('acf/create_field', array( 
  99. 'type' => 'number',  
  100. 'name' => 'fields['.$key.'][default_value]',  
  101. 'value' => $field['default_value'],  
  102. )); 
  103.  
  104. ?> 
  105. </td> 
  106. </tr> 
  107. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  108. <td class="label"> 
  109. <label><?php _e("Placeholder Text", 'acf'); ?></label> 
  110. <p><?php _e("Appears within the input", 'acf') ?></p> 
  111. </td> 
  112. <td> 
  113. <?php  
  114. do_action('acf/create_field', array( 
  115. 'type' => 'text',  
  116. 'name' => 'fields[' .$key.'][placeholder]',  
  117. 'value' => $field['placeholder'],  
  118. )); 
  119. ?> 
  120. </td> 
  121. </tr> 
  122. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  123. <td class="label"> 
  124. <label><?php _e("Prepend", 'acf'); ?></label> 
  125. <p><?php _e("Appears before the input", 'acf') ?></p> 
  126. </td> 
  127. <td> 
  128. <?php  
  129. do_action('acf/create_field', array( 
  130. 'type' => 'text',  
  131. 'name' => 'fields[' .$key.'][prepend]',  
  132. 'value' => $field['prepend'],  
  133. )); 
  134. ?> 
  135. </td> 
  136. </tr> 
  137. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  138. <td class="label"> 
  139. <label><?php _e("Append", 'acf'); ?></label> 
  140. <p><?php _e("Appears after the input", 'acf') ?></p> 
  141. </td> 
  142. <td> 
  143. <?php  
  144. do_action('acf/create_field', array( 
  145. 'type' => 'text',  
  146. 'name' => 'fields[' .$key.'][append]',  
  147. 'value' => $field['append'],  
  148. )); 
  149. ?> 
  150. </td> 
  151. </tr> 
  152. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  153. <td class="label"> 
  154. <label><?php _e("Minimum Value", 'acf'); ?></label> 
  155. </td> 
  156. <td> 
  157. <?php 
  158.  
  159. do_action('acf/create_field', array( 
  160. 'type' => 'number',  
  161. 'name' => 'fields['.$key.'][min]',  
  162. 'value' => $field['min'],  
  163. )); 
  164.  
  165. ?> 
  166. </td> 
  167. </tr> 
  168. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  169. <td class="label"> 
  170. <label><?php _e("Maximum Value", 'acf'); ?></label> 
  171. </td> 
  172. <td> 
  173. <?php 
  174.  
  175. do_action('acf/create_field', array( 
  176. 'type' => 'number',  
  177. 'name' => 'fields['.$key.'][max]',  
  178. 'value' => $field['max'],  
  179. )); 
  180.  
  181. ?> 
  182. </td> 
  183. </tr> 
  184. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  185. <td class="label"> 
  186. <label><?php _e("Step Size", 'acf'); ?></label> 
  187. </td> 
  188. <td> 
  189. <?php 
  190.  
  191. do_action('acf/create_field', array( 
  192. 'type' => 'number',  
  193. 'name' => 'fields['.$key.'][step]',  
  194. 'value' => $field['step'],  
  195. )); 
  196.  
  197. ?> 
  198. </td> 
  199. </tr> 
  200. <?php 
  201.  
  202.  
  203. /** 
  204. * update_value() 
  205. * This filter is appied to the $value before it is updated in the db 
  206. * @type filter 
  207. * @since 3.6 
  208. * @date 23/01/13 
  209. * @param $value - the value which will be saved in the database 
  210. * @param $field - the field array holding all the field options 
  211. * @param $post_id - the $post_id of which the value will be saved 
  212. * @return $value - the modified value 
  213. */ 
  214.  
  215. function update_value( $value, $post_id, $field ) 
  216. // no formatting needed for empty value 
  217. if( empty($value) ) { 
  218.  
  219. return $value; 
  220.  
  221.  
  222.  
  223. // remove ', ' 
  224. $value = str_replace(', ', '', $value); 
  225.  
  226.  
  227. // convert to float. This removes any chars 
  228. $value = floatval( $value ); 
  229.  
  230.  
  231. // convert back to string. This alows decimals to save 
  232. $value = (string) $value; 
  233.  
  234.  
  235. return $value; 
  236.  
  237.