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.2.1 4.2.2 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.10 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 5.10 5.10.1 5.10.2 5.11 5.11.1 5.11.2 5.11.3 5.11.4 5.12 5.12.1 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.6.10 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.10 5.7.12 5.7.13 5.7.2 5.7.3 5.7.4 5.7.5 5.7.6 5.7.7 5.7.8 5.7.9 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.2 5.8.3 5.8.4
advanced-custom-fields / includes / fields / class-acf-field-number.php
advanced-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 5 months ago class-acf-field-button-group.php 5 months ago class-acf-field-checkbox.php 2 months ago class-acf-field-color_picker.php 5 months ago class-acf-field-date_picker.php 5 months ago class-acf-field-date_time_picker.php 5 months ago class-acf-field-email.php 5 months ago class-acf-field-file.php 5 months ago class-acf-field-google-map.php 5 months ago class-acf-field-group.php 5 months ago class-acf-field-icon_picker.php 6 months ago class-acf-field-image.php 1 month ago class-acf-field-link.php 5 months ago class-acf-field-message.php 6 months ago class-acf-field-number.php 5 months ago class-acf-field-oembed.php 2 months ago class-acf-field-output.php 6 months ago class-acf-field-page_link.php 1 month ago class-acf-field-password.php 5 months ago class-acf-field-post_object.php 1 month ago class-acf-field-radio.php 2 months ago class-acf-field-range.php 5 months ago class-acf-field-relationship.php 1 month ago class-acf-field-select.php 2 months ago class-acf-field-separator.php 6 months ago class-acf-field-tab.php 6 months ago class-acf-field-taxonomy.php 2 months ago class-acf-field-text.php 5 months ago class-acf-field-textarea.php 5 months ago class-acf-field-time_picker.php 5 months ago class-acf-field-true_false.php 5 months ago class-acf-field-url.php 5 months ago class-acf-field-user.php 2 months ago class-acf-field-wysiwyg.php 5 months ago class-acf-field.php 5 months ago index.php 2 years ago
class-acf-field-number.php
341 lines
1 <?php
2 /**
3 * @package ACF
4 * @author WP Engine
5 *
6 * © 2026 Advanced Custom Fields (ACF®). All rights reserved.
7 * "ACF" is a trademark of WP Engine.
8 * Licensed under the GNU General Public License v2 or later.
9 * https://www.gnu.org/licenses/gpl-2.0.html
10 */
11
12 if ( ! class_exists( 'acf_field_number' ) ) :
13
14 class acf_field_number extends acf_field {
15
16
17 /**
18 * This function will setup the field type data
19 *
20 * @type function
21 * @date 5/03/2014
22 * @since 5.0.0
23 *
24 * @param n/a
25 * @return n/a
26 */
27 function initialize() {
28
29 // vars
30 $this->name = 'number';
31 $this->label = __( 'Number', 'acf' );
32 $this->description = __( 'An input limited to numerical values.', 'acf' );
33 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-number.png';
34 $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/number/', 'docs', 'field-type-selection' );
35 $this->defaults = array(
36 'default_value' => '',
37 'min' => '',
38 'max' => '',
39 'step' => '',
40 'placeholder' => '',
41 'prepend' => '',
42 'append' => '',
43 );
44 }
45
46
47 /**
48 * Create the HTML interface for your field
49 *
50 * @param $field - an array holding all the field's data
51 *
52 * @type action
53 * @since 3.6
54 * @date 23/01/13
55 */
56 function render_field( $field ) {
57
58 // vars
59 $atts = array();
60 $keys = array( 'type', 'id', 'class', 'name', 'value', 'min', 'max', 'step', 'placeholder', 'pattern' );
61 $keys2 = array( 'readonly', 'disabled', 'required' );
62 $html = '';
63
64 // step
65 if ( ! $field['step'] ) {
66 $field['step'] = 'any';
67 }
68
69 // prepend
70 if ( $field['prepend'] !== '' ) {
71 $field['class'] .= ' acf-is-prepended';
72 $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
73 }
74
75 // append
76 if ( $field['append'] !== '' ) {
77 $field['class'] .= ' acf-is-appended';
78 $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
79 }
80
81 // atts (value="123")
82 foreach ( $keys as $k ) {
83 if ( isset( $field[ $k ] ) ) {
84 $atts[ $k ] = $field[ $k ];
85 }
86 }
87
88 // atts2 (disabled="disabled")
89 foreach ( $keys2 as $k ) {
90 if ( ! empty( $field[ $k ] ) ) {
91 $atts[ $k ] = $k;
92 }
93 }
94
95 // remove empty atts
96 $atts = acf_clean_atts( $atts );
97
98 // render
99 $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
100
101 // return
102 echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped by individual html functions above.
103 }
104
105
106 /**
107 * Create extra options for your field. This is rendered when editing a field.
108 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
109 *
110 * @type action
111 * @since 3.6
112 * @date 23/01/13
113 *
114 * @param $field - an array holding all the field's data
115 */
116 function render_field_settings( $field ) {
117
118 // default_value
119 acf_render_field_setting(
120 $field,
121 array(
122 'label' => __( 'Default Value', 'acf' ),
123 'instructions' => __( 'Appears when creating a new post', 'acf' ),
124 'type' => 'text',
125 'name' => 'default_value',
126 )
127 );
128 }
129
130 /**
131 * Renders the field settings used in the "Validation" tab.
132 *
133 * @since 6.0
134 *
135 * @param array $field The field settings array.
136 * @return void
137 */
138 function render_field_validation_settings( $field ) {
139 acf_render_field_setting(
140 $field,
141 array(
142 'label' => __( 'Minimum Value', 'acf' ),
143 'instructions' => '',
144 'type' => 'number',
145 'name' => 'min',
146 )
147 );
148
149 acf_render_field_setting(
150 $field,
151 array(
152 'label' => __( 'Maximum Value', 'acf' ),
153 'instructions' => '',
154 'type' => 'number',
155 'name' => 'max',
156 )
157 );
158 }
159
160 /**
161 * Renders the field settings used in the "Presentation" tab.
162 *
163 * @since 6.0
164 *
165 * @param array $field The field settings array.
166 * @return void
167 */
168 function render_field_presentation_settings( $field ) {
169 acf_render_field_setting(
170 $field,
171 array(
172 'label' => __( 'Placeholder Text', 'acf' ),
173 'instructions' => __( 'Appears within the input', 'acf' ),
174 'type' => 'text',
175 'name' => 'placeholder',
176 )
177 );
178
179 acf_render_field_setting(
180 $field,
181 array(
182 'label' => __( 'Step Size', 'acf' ),
183 'instructions' => '',
184 'type' => 'number',
185 'name' => 'step',
186 )
187 );
188
189 acf_render_field_setting(
190 $field,
191 array(
192 'label' => __( 'Prepend', 'acf' ),
193 'instructions' => __( 'Appears before the input', 'acf' ),
194 'type' => 'text',
195 'name' => 'prepend',
196 )
197 );
198
199 acf_render_field_setting(
200 $field,
201 array(
202 'label' => __( 'Append', 'acf' ),
203 'instructions' => __( 'Appears after the input', 'acf' ),
204 'type' => 'text',
205 'name' => 'append',
206 )
207 );
208 }
209
210 /**
211 * description
212 *
213 * @type function
214 * @date 11/02/2014
215 * @since 5.0.0
216 *
217 * @param $post_id (int)
218 * @return $post_id (int)
219 */
220 function validate_value( $valid, $value, $field, $input ) {
221
222 // remove ','
223 if ( acf_str_exists( ',', $value ) ) {
224 $value = str_replace( ',', '', $value );
225 }
226
227 // if value is not numeric...
228 if ( ! is_numeric( $value ) ) {
229
230 // allow blank to be saved
231 if ( ! empty( $value ) ) {
232 $valid = __( 'Value must be a number', 'acf' );
233 }
234
235 // return early
236 return $valid;
237 }
238
239 // convert
240 $value = floatval( $value );
241
242 // min
243 if ( is_numeric( $field['min'] ) && $value < floatval( $field['min'] ) ) {
244 $valid = sprintf( __( 'Value must be equal to or higher than %d', 'acf' ), $field['min'] );
245 }
246
247 // max
248 if ( is_numeric( $field['max'] ) && $value > floatval( $field['max'] ) ) {
249 $valid = sprintf( __( 'Value must be equal to or lower than %d', 'acf' ), $field['max'] );
250 }
251
252 // return
253 return $valid;
254 }
255
256
257 /**
258 * This filter is appied to the $value before it is updated in the db
259 *
260 * @type filter
261 * @since 3.6
262 * @date 23/01/13
263 *
264 * @param $value - the value which will be saved in the database
265 * @param $field - the field array holding all the field options
266 * @param $post_id - the post_id of which the value will be saved
267 *
268 * @return $value - the modified value
269 */
270 function update_value( $value, $post_id, $field ) {
271
272 // no formatting needed for empty value
273 if ( empty( $value ) ) {
274 return $value;
275 }
276
277 // remove ','
278 if ( acf_str_exists( ',', $value ) ) {
279 $value = str_replace( ',', '', $value );
280 }
281
282 // return
283 return $value;
284 }
285
286 /**
287 * Return the schema array for the REST API.
288 *
289 * @param array $field
290 * @return array
291 */
292 public function get_rest_schema( array $field ) {
293 $schema = array(
294 'type' => array( 'number', 'null' ),
295 'required' => ! empty( $field['required'] ),
296 );
297
298 if ( ! empty( $field['min'] ) ) {
299 $schema['minimum'] = (float) $field['min'];
300 }
301
302 if ( ! empty( $field['max'] ) ) {
303 $schema['maximum'] = (float) $field['max'];
304 }
305
306 if ( isset( $field['default_value'] ) && is_numeric( $field['default_value'] ) ) {
307 $schema['default'] = (float) $field['default_value'];
308 }
309
310 return $schema;
311 }
312
313 /**
314 * Apply basic formatting to prepare the value for default REST output.
315 *
316 * @param mixed $value
317 * @param string|integer $post_id
318 * @param array $field
319 * @return mixed
320 */
321 public function format_value_for_rest( $value, $post_id, array $field ) {
322 return acf_format_numerics( $value );
323 }
324
325 /**
326 * Returns an array of JSON-LD Property output types that are supported by this field type.
327 *
328 * @since 6.8
329 *
330 * @return string[]
331 */
332 public function get_jsonld_output_types(): array {
333 return array( 'Number', 'Integer' );
334 }
335 }
336
337
338 // initialize
339 acf_register_field_type( 'acf_field_number' );
340 endif; // class_exists check
341