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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-true_false.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-true_false.php
322 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_true_false' ) ) :
13
14 class acf_field_true_false 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 = 'true_false';
31 $this->label = __( 'True / False', 'acf' );
32 $this->category = 'choice';
33 $this->description = __( 'A toggle that allows you to pick a value of 1 or 0 (on or off, true or false, etc). Can be presented as a stylized switch or checkbox.', 'acf' );
34 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-true-false.png';
35 $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/true-false/', 'docs', 'field-type-selection' );
36 $this->defaults = array(
37 'default_value' => 0,
38 'message' => '',
39 'ui' => 0,
40 'ui_on_text' => '',
41 'ui_off_text' => '',
42 );
43 }
44
45
46 /**
47 * Create the HTML interface for your field
48 *
49 * @param $field - an array holding all the field's data
50 *
51 * @type action
52 * @since 3.6
53 * @date 23/01/13
54 */
55 function render_field( $field ) {
56
57 // vars
58 $input = array(
59 'type' => 'checkbox',
60 'id' => $field['id'],
61 'name' => $field['name'],
62 'value' => '1',
63 'class' => $field['class'],
64 'autocomplete' => 'off',
65 );
66
67 $hidden = array(
68 'name' => $field['name'],
69 'value' => 0,
70 );
71
72 $active = $field['value'] ? true : false;
73 $switch = '';
74
75 // checked
76 if ( $active ) {
77 $input['checked'] = 'checked';
78 }
79
80 // ui
81 if ( $field['ui'] ) {
82
83 // vars
84 if ( $field['ui_on_text'] === '' ) {
85 $field['ui_on_text'] = __( 'Yes', 'acf' );
86 }
87 if ( $field['ui_off_text'] === '' ) {
88 $field['ui_off_text'] = __( 'No', 'acf' );
89 }
90
91 // update input
92 $input['class'] .= ' acf-switch-input';
93 // $input['style'] = 'display:none;';
94 $switch .= '<div class="acf-switch' . ( $active ? ' -on' : '' ) . '">';
95 $switch .= '<span class="acf-switch-on">' . $field['ui_on_text'] . '</span>';
96 $switch .= '<span class="acf-switch-off">' . $field['ui_off_text'] . '</span>';
97 $switch .= '<div class="acf-switch-slider"></div>';
98 $switch .= '</div>';
99 }
100
101 ?>
102 <div class="acf-true-false">
103 <?php acf_hidden_input( $hidden ); ?>
104 <label>
105 <input <?php echo acf_esc_attrs( $input ); ?>/>
106 <?php
107 if ( $switch ) {
108 echo acf_esc_html( $switch );}
109 ?>
110 <?php
111 if ( $field['message'] ) :
112 ?>
113 <span class="message"><?php echo acf_esc_html( $field['message'] ); ?></span><?php endif; ?>
114 </label>
115 </div>
116 <?php
117 }
118
119
120 /**
121 * Create extra options for your field. This is rendered when editing a field.
122 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
123 *
124 * @type action
125 * @since 3.6
126 * @date 23/01/13
127 *
128 * @param $field - an array holding all the field's data
129 */
130 function render_field_settings( $field ) {
131 acf_render_field_setting(
132 $field,
133 array(
134 'label' => __( 'Message', 'acf' ),
135 'instructions' => __( 'Displays text alongside the checkbox', 'acf' ),
136 'type' => 'text',
137 'name' => 'message',
138 )
139 );
140
141 acf_render_field_setting(
142 $field,
143 array(
144 'label' => __( 'Default Value', 'acf' ),
145 'instructions' => '',
146 'type' => 'true_false',
147 'name' => 'default_value',
148 )
149 );
150 }
151
152 /**
153 * Renders the field settings used in the "Presentation" tab.
154 *
155 * @since 6.0
156 *
157 * @param array $field The field settings array.
158 * @return void
159 */
160 function render_field_presentation_settings( $field ) {
161 acf_render_field_setting(
162 $field,
163 array(
164 'label' => __( 'On Text', 'acf' ),
165 'instructions' => __( 'Text shown when active', 'acf' ),
166 'type' => 'text',
167 'name' => 'ui_on_text',
168 'placeholder' => __( 'Yes', 'acf' ),
169 'conditions' => array(
170 'field' => 'ui',
171 'operator' => '==',
172 'value' => 1,
173 ),
174 )
175 );
176
177 acf_render_field_setting(
178 $field,
179 array(
180 'label' => __( 'Off Text', 'acf' ),
181 'instructions' => __( 'Text shown when inactive', 'acf' ),
182 'type' => 'text',
183 'name' => 'ui_off_text',
184 'placeholder' => __( 'No', 'acf' ),
185 'conditions' => array(
186 'field' => 'ui',
187 'operator' => '==',
188 'value' => 1,
189 ),
190 )
191 );
192
193 acf_render_field_setting(
194 $field,
195 array(
196 'label' => __( 'Stylized UI', 'acf' ),
197 'instructions' => __( 'Use a stylized checkbox using select2', 'acf' ),
198 'type' => 'true_false',
199 'name' => 'ui',
200 'ui' => 1,
201 'class' => 'acf-field-object-true-false-ui',
202 )
203 );
204 }
205
206 /**
207 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
208 *
209 * @type filter
210 * @since 3.6
211 * @date 23/01/13
212 *
213 * @param $value (mixed) the value which was loaded from the database
214 * @param $post_id (mixed) the post_id from which the value was loaded
215 * @param $field (array) the field array holding all the field options
216 *
217 * @return $value (mixed) the modified value
218 */
219 function format_value( $value, $post_id, $field ) {
220
221 return empty( $value ) ? false : true;
222 }
223
224
225 /**
226 * description
227 *
228 * @type function
229 * @date 11/02/2014
230 * @since 5.0.0
231 *
232 * @param $post_id (int)
233 * @return $post_id (int)
234 */
235 function validate_value( $valid, $value, $field, $input ) {
236
237 // bail early if not required
238 if ( ! $field['required'] ) {
239 return $valid;
240 }
241
242 // value may be '0'
243 if ( ! $value ) {
244 return false;
245 }
246
247 // return
248 return $valid;
249 }
250
251
252 /**
253 * This function will translate field settings
254 *
255 * @type function
256 * @date 8/03/2016
257 * @since 5.3.2
258 *
259 * @param $field (array)
260 * @return $field
261 */
262 function translate_field( $field ) {
263
264 // translate
265 $field['message'] = acf_translate( $field['message'] );
266 $field['ui_on_text'] = acf_translate( $field['ui_on_text'] );
267 $field['ui_off_text'] = acf_translate( $field['ui_off_text'] );
268
269 // return
270 return $field;
271 }
272
273 /**
274 * Return the schema array for the REST API.
275 *
276 * @param array $field
277 * @return array
278 */
279 public function get_rest_schema( array $field ) {
280 $schema = array(
281 'type' => array( 'boolean', 'null' ),
282 'required' => ! empty( $field['required'] ),
283 );
284
285 if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
286 $schema['default'] = (bool) $field['default_value'];
287 }
288
289 return $schema;
290 }
291
292 /**
293 * Apply basic formatting to prepare the value for default REST output.
294 *
295 * @param mixed $value
296 * @param string|integer $post_id
297 * @param array $field
298 * @return mixed
299 */
300 public function format_value_for_rest( $value, $post_id, array $field ) {
301 return (bool) $value;
302 }
303
304 /**
305 * Returns an array of JSON-LD Property output types that are supported by this field type.
306 *
307 * @since 6.8
308 *
309 * @return string[]
310 */
311 public function get_jsonld_output_types(): array {
312 return array( 'Boolean' );
313 }
314 }
315
316
317 // initialize
318 acf_register_field_type( 'acf_field_true_false' );
319 endif; // class_exists check
320
321 ?>
322