PluginProbe
Advanced Custom Fields (ACF®) / 5.6.6
Advanced Custom Fields (ACF®) v5.6.6
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / api / api-template.php
api-template.php
1,543 lines 27.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * acf_get_field_reference()
5 *
6 * This function will find the $field_key that is related to the $field_name.
7 * This is know as the field value reference
8 *
9 * @type function
10 * @since 3.6
11 * @date 29/01/13
12 *
13 * @param $field_name (mixed) the name of the field. eg 'sub_heading'
14 * @param $post_id (int) the post_id of which the value is saved against
15 * @return $reference (string) a string containing the field_key
16 */
17
18 function acf_get_field_reference( $field_name, $post_id ) {
19
20 // vars
21 $field_key = acf_get_metadata( $post_id, $field_name, true );
22
23
24 // filter
25 $field_key = apply_filters('acf/get_field_reference', $field_key, $field_name, $post_id);
26
27
28 // return
29 return $field_key;
30
31 }
32
33
34 /*
35 * get_field()
36 *
37 * This function will return a custom field value for a specific field name/key + post_id.
38 * There is a 3rd parameter to turn on/off formating. This means that an image field will not use
39 * its 'return option' to format the value but return only what was saved in the database
40 *
41 * @type function
42 * @since 3.6
43 * @date 29/01/13
44 *
45 * @param $selector (string) the field name or key
46 * @param $post_id (mixed) the post_id of which the value is saved against
47 * @param $format_value (boolean) whether or not to format the value as described above
48 * @return (mixed)
49 */
50
51 function get_field( $selector, $post_id = false, $format_value = true ) {
52
53 // filter post_id
54 $post_id = acf_get_valid_post_id( $post_id );
55
56
57 // get field
58 $field = acf_maybe_get_field( $selector, $post_id );
59
60
61 // create dummy field
62 if( !$field ) {
63
64 $field = acf_get_valid_field(array(
65 'name' => $selector,
66 'key' => '',
67 'type' => '',
68 ));
69
70
71 // prevent formatting
72 $format_value = false;
73
74 }
75
76
77 // get value for field
78 $value = acf_get_value( $post_id, $field );
79
80
81 // format value
82 if( $format_value ) {
83
84 // get value for field
85 $value = acf_format_value( $value, $post_id, $field );
86
87 }
88
89
90 // return
91 return $value;
92
93 }
94
95
96 /*
97 * the_field()
98 *
99 * This function is the same as echo get_field().
100 *
101 * @type function
102 * @since 1.0.3
103 * @date 29/01/13
104 *
105 * @param $selector (string) the field name or key
106 * @param $post_id (mixed) the post_id of which the value is saved against
107 * @return n/a
108 */
109
110 function the_field( $selector, $post_id = false, $format_value = true ) {
111
112 $value = get_field($selector, $post_id, $format_value);
113
114 if( is_array($value) ) {
115
116 $value = @implode( ', ', $value );
117
118 }
119
120 echo $value;
121
122 }
123
124
125 /*
126 * get_field_object()
127 *
128 * This function will return an array containing all the field data for a given field_name
129 *
130 * @type function
131 * @since 3.6
132 * @date 3/02/13
133 *
134 * @param $selector (string) the field name or key
135 * @param $post_id (mixed) the post_id of which the value is saved against
136 * @param $format_value (boolean) whether or not to format the field value
137 * @param $load_value (boolean) whether or not to load the field value
138 * @return $field (array)
139 */
140
141 function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true ) {
142
143 // compatibilty
144 if( is_array($format_value) ) extract( $format_value );
145
146
147 // get valid post_id
148 $post_id = acf_get_valid_post_id( $post_id );
149
150
151 // get field key
152 $field = acf_maybe_get_field( $selector, $post_id );
153
154
155 // bail early if no field found
156 if( !$field ) return false;
157
158
159 // load value
160 if( $load_value ) {
161
162 $field['value'] = acf_get_value( $post_id, $field );
163
164 }
165
166
167 // format value
168 if( $format_value ) {
169
170 // get value for field
171 $field['value'] = acf_format_value( $field['value'], $post_id, $field );
172
173 }
174
175
176 // return
177 return $field;
178
179 }
180
181
182 /*
183 * get_fields()
184 *
185 * This function will return an array containing all the custom field values for a specific post_id.
186 * The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the values.
187 *
188 * @type function
189 * @since 3.6
190 * @date 29/01/13
191 *
192 * @param $post_id (mixed) the post_id of which the value is saved against
193 * @param $format_value (boolean) whether or not to format the field value
194 * @return (array) associative array where field name => field value
195 */
196
197 function get_fields( $post_id = false, $format_value = true ) {
198
199 // vars
200 $fields = get_field_objects( $post_id, $format_value );
201 $meta = array();
202
203
204 // bail early
205 if( !$fields ) return false;
206
207
208 // populate
209 foreach( $fields as $k => $field ) {
210
211 $meta[ $k ] = $field['value'];
212
213 }
214
215
216 // return
217 return $meta;
218
219 }
220
221
222 /*
223 * get_field_objects()
224 *
225 * This function will return an array containing all the custom field objects for a specific post_id.
226 * The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.
227 *
228 * @type function
229 * @since 3.6
230 * @date 29/01/13
231 *
232 * @param $post_id (mixed) the post_id of which the value is saved against
233 * @param $format_value (boolean) whether or not to format the field value
234 * @param $load_value (boolean) whether or not to load the field value
235 * @return (array) associative array where field name => field
236 */
237
238 function get_field_objects( $post_id = false, $format_value = true, $load_value = true ) {
239
240 // global
241 global $wpdb;
242
243
244 // filter post_id
245 $post_id = acf_get_valid_post_id( $post_id );
246 $info = acf_get_post_id_info( $post_id );
247
248
249 // vars
250 $meta = array();
251 $fields = array();
252
253
254 // get field_names
255 if( $info['type'] == 'post' ) {
256
257 $meta = get_post_meta( $info['id'] );
258
259 } elseif( $info['type'] == 'user' ) {
260
261 $meta = get_user_meta( $info['id'] );
262
263 } elseif( $info['type'] == 'comment' ) {
264
265 $meta = get_comment_meta( $info['id'] );
266
267 } elseif( $info['type'] == 'term' ) {
268
269 $meta = get_term_meta( $info['id'] );
270
271 } else {
272
273 $rows = $wpdb->get_results($wpdb->prepare(
274 "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
275 $post_id . '_%' ,
276 '_' . $post_id . '_%'
277 ), ARRAY_A);
278
279 if( !empty($rows) ) {
280
281 foreach( $rows as $row ) {
282
283 // vars
284 $name = $row['option_name'];
285 $prefix = $post_id . '_';
286 $_prefix = '_' . $prefix;
287
288
289 // remove prefix from name
290 if( strpos($name, $prefix) === 0 ) {
291
292 $name = substr($name, strlen($prefix));
293
294 } elseif( strpos($name, $_prefix) === 0 ) {
295
296 $name = '_' . substr($name, strlen($_prefix));
297
298 }
299
300 $meta[ $name ][] = $row['option_value'];
301
302 }
303
304 }
305
306 }
307
308
309 // bail early if no meta
310 if( empty($meta) ) return false;
311
312
313 // populate vars
314 foreach( $meta as $k => $v ) {
315
316 // does a field key exist for this value?
317 if( !isset($meta["_{$k}"]) ) continue;
318
319
320 // get field
321 $field_key = $meta["_{$k}"][0];
322 $field = acf_maybe_get_field( $field_key );
323
324
325 // bail early if no field, or if the field's name is different to $k
326 // - solves problem where sub fields (and clone fields) are incorrectly allowed
327 if( !$field || $field['name'] !== $k ) continue;
328
329
330 // load value
331 if( $load_value ) {
332
333 $field['value'] = acf_get_value( $post_id, $field );
334
335 }
336
337
338 // format value
339 if( $format_value ) {
340
341 // get value for field
342 $field['value'] = acf_format_value( $field['value'], $post_id, $field );
343
344 }
345
346
347 // append to $value
348 $fields[ $field['name'] ] = $field;
349
350 }
351
352
353 // no value
354 if( empty($fields) ) return false;
355
356
357 // return
358 return $fields;
359 }
360
361
362 /*
363 * have_rows
364 *
365 * This function will instantiate a global variable containing the rows of a repeater or flexible content field,
366 * after which, it will determine if another row exists to loop through
367 *
368 * @type function
369 * @date 2/09/13
370 * @since 4.3.0
371 *
372 * @param $field_name (string) the field name
373 * @param $post_id (mixed) the post_id of which the value is saved against
374 * @return (boolean)
375 */
376
377 function have_rows( $selector, $post_id = false ) {
378
379 // reference
380 $_post_id = $post_id;
381
382
383 // filter post_id
384 $post_id = acf_get_valid_post_id( $post_id );
385
386
387 // vars
388 $key = "selector={$selector}/post_id={$post_id}";
389 $active_loop = acf_get_loop('active');
390 $previous_loop = acf_get_loop('previous');
391 $new_parent_loop = false;
392 $new_child_loop = false;
393 $sub_field = false;
394 $sub_exists = false;
395 $change = false;
396
397
398 // no active loops
399 if( !$active_loop ) {
400
401 // create a new loop
402 $new_parent_loop = true;
403
404 // loop has changed
405 } elseif( $active_loop['key'] != $key ) {
406
407 // detect change
408 if( $post_id != $active_loop['post_id'] ) {
409
410 $change = 'post_id';
411
412 } elseif( $selector != $active_loop['selector'] ) {
413
414 $change = 'selector';
415
416 } else {
417
418 // key has changed due to a technicallity, however, the post_id and selector are the same
419
420 }
421
422
423 // attempt to find sub field
424 $sub_field = acf_get_sub_field($selector, $active_loop['field']);
425
426 if( $sub_field ) {
427
428 $sub_exists = isset( $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ] );
429
430 }
431
432
433 // If post_id has changed, this is most likely an archive loop
434 if( $change == 'post_id' ) {
435
436 if( empty($_post_id) && $sub_exists ) {
437
438 // case: Change in $post_id was due to this being a nested loop and not specifying the $post_id
439 // action: move down one level into a new loop
440 $new_child_loop = true;
441
442 } elseif( $previous_loop && $previous_loop['post_id'] == $post_id ) {
443
444 // case: Change in $post_id was due to a nested loop ending
445 // action: move up one level through the loops
446 acf_remove_loop('active');
447
448 } else {
449
450 // case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects
451 // action: leave this current loop alone and create a new parent loop
452 $new_parent_loop = true;
453
454 }
455
456 } elseif( $change == 'selector' ) {
457
458 if( $sub_exists ) {
459
460 // case: Change in $field_name was due to this being a nested loop
461 // action: move down one level into a new loop
462 $new_child_loop = true;
463
464 } elseif( $previous_loop && $previous_loop['selector'] == $selector && $previous_loop['post_id'] == $post_id ) {
465
466 // case: Change in $field_name was due to a nested loop ending
467 // action: move up one level through the loops
468 acf_remove_loop('active');
469
470 } else {
471
472 // case: Chang in $field_name is the most obvious, this is a new loop for a different field within the $post
473 // action: leave this current loop alone and create a new parent loop
474 $new_parent_loop = true;
475
476 }
477
478 }
479
480 // loop is the same
481 } else {
482
483 // do nothing
484
485 }
486
487
488 // add loop
489 if( $new_parent_loop || $new_child_loop ) {
490
491 // vars
492 $field = null;
493 $value = null;
494 $name = '';
495
496
497 // parent loop
498 if( $new_parent_loop ) {
499
500 $field = get_field_object( $selector, $post_id, false );
501 $value = acf_extract_var( $field, 'value' );
502 $name = $field['name'];
503
504 // child loop
505 } else {
506
507 $field = $sub_field;
508 $value = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ];
509 $name = $active_loop['name'] . '_' . $active_loop['i'] . '_' . $sub_field['name'];
510 $post_id = $active_loop['post_id'];
511
512 }
513
514
515 // bail early if value is either empty or a non array
516 if( !acf_is_array($value) ) return false;
517
518
519 // allow for non repeatable data (group)
520 if( acf_get_field_type_prop($field['type'], 'have_rows') === 'single' ) {
521 $value = array( $value );
522 }
523
524
525 // add loop
526 $active_loop = acf_add_loop(array(
527 'selector' => $selector,
528 'name' => $name, // used by update_sub_field
529 'value' => $value,
530 'field' => $field,
531 'i' => -1,
532 'post_id' => $post_id,
533 'key' => $key
534 ));
535
536 }
537
538
539 // return true if next row exists
540 if( $active_loop && isset($active_loop['value'][ $active_loop['i']+1 ]) ) {
541
542 return true;
543
544 }
545
546
547 // no next row!
548 acf_remove_loop('active');
549
550
551 // return
552 return false;
553
554 }
555
556
557 /*
558 * the_row
559 *
560 * This function will progress the global repeater or flexible content value 1 row
561 *
562 * @type function
563 * @date 2/09/13
564 * @since 4.3.0
565 *
566 * @param N/A
567 * @return (array) the current row data
568 */
569
570 function the_row( $format = false ) {
571
572 // vars
573 $i = acf_get_loop('active', 'i');
574
575
576 // increase
577 $i++;
578
579
580 // update
581 acf_update_loop('active', 'i', $i);
582
583
584 // return
585 return get_row( $format );
586
587 }
588
589 function get_row( $format = false ) {
590
591 // vars
592 $loop = acf_get_loop('active');
593
594
595 // bail early if no loop
596 if( !$loop ) return false;
597
598
599 // get value
600 $value = acf_maybe_get( $loop['value'], $loop['i'] );
601
602
603 // bail early if no current value
604 // possible if get_row_layout() is called before the_row()
605 if( !$value ) return false;
606
607
608 // format
609 if( $format ) {
610
611 // vars
612 $field = $loop['field'];
613
614
615 // single row
616 if( acf_get_field_type_prop($field['type'], 'have_rows') === 'single' ) {
617
618 // format value
619 $value = acf_format_value( $value, $loop['post_id'], $field );
620
621 // multiple rows
622 } else {
623
624 // format entire value
625 // - solves problem where cached value is incomplete
626 // - no performance issues here thanks to cache
627 $value = acf_format_value( $loop['value'], $loop['post_id'], $field );
628 $value = acf_maybe_get( $value, $loop['i'] );
629
630 }
631
632 }
633
634
635 // return
636 return $value;
637
638 }
639
640 function get_row_index() {
641
642 // vars
643 $i = acf_get_loop('active', 'i');
644 $offset = acf_get_setting('row_index_offset');
645
646
647 // return
648 return $offset + $i;
649
650 }
651
652 function the_row_index() {
653
654 echo get_row_index();
655
656 }
657
658
659 /*
660 * get_row_sub_field
661 *
662 * This function is used inside a 'has_sub_field' while loop to return a sub field object
663 *
664 * @type function
665 * @date 16/05/2016
666 * @since 5.3.8
667 *
668 * @param $selector (string)
669 * @return (array)
670 */
671
672 function get_row_sub_field( $selector ) {
673
674 // vars
675 $row = acf_get_loop('active');
676
677
678 // bail early if no row
679 if( !$row ) return false;
680
681
682 // attempt to find sub field
683 $sub_field = acf_get_sub_field($selector, $row['field']);
684
685
686 // bail early if no field
687 if( !$sub_field ) return false;
688
689
690 // update field's name based on row data
691 $sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}";
692
693
694 // return
695 return $sub_field;
696
697 }
698
699
700 /*
701 * get_row_sub_value
702 *
703 * This function is used inside a 'has_sub_field' while loop to return a sub field value
704 *
705 * @type function
706 * @date 16/05/2016
707 * @since 5.3.8
708 *
709 * @param $selector (string)
710 * @return (mixed)
711 */
712
713 function get_row_sub_value( $selector ) {
714
715 // vars
716 $row = acf_get_loop('active');
717
718
719 // bail early if no row
720 if( !$row ) return null;
721
722
723 // return value
724 if( isset($row['value'][ $row['i'] ][ $selector ]) ) {
725
726 return $row['value'][ $row['i'] ][ $selector ];
727
728 }
729
730
731 // return
732 return null;
733
734 }
735
736
737 /*
738 * reset_rows
739 *
740 * This function will find the current loop and unset it from the global array.
741 * To bo used when loop finishes or a break is used
742 *
743 * @type function
744 * @date 26/10/13
745 * @since 5.0.0
746 *
747 * @param $hard_reset (boolean) completely wipe the global variable, or just unset the active row
748 * @return (boolean)
749 */
750
751 function reset_rows() {
752
753 // remove last loop
754 acf_remove_loop('active');
755
756
757 // return
758 return true;
759
760 }
761
762
763 /*
764 * has_sub_field()
765 *
766 * This function is used inside a while loop to return either true or false (loop again or stop).
767 * When using a repeater or flexible content field, it will loop through the rows until
768 * there are none left or a break is detected
769 *
770 * @type function
771 * @since 1.0.3
772 * @date 29/01/13
773 *
774 * @param $field_name (string) the field name
775 * @param $post_id (mixed) the post_id of which the value is saved against
776 * @return (boolean)
777 */
778
779 function has_sub_field( $field_name, $post_id = false ) {
780
781 // vars
782 $r = have_rows( $field_name, $post_id );
783
784
785 // if has rows, progress through 1 row for the while loop to work
786 if( $r ) {
787
788 the_row();
789
790 }
791
792
793 // return
794 return $r;
795
796 }
797
798 function has_sub_fields( $field_name, $post_id = false ) {
799
800 return has_sub_field( $field_name, $post_id );
801
802 }
803
804
805 /*
806 * get_sub_field()
807 *
808 * This function is used inside a 'has_sub_field' while loop to return a sub field value
809 *
810 * @type function
811 * @since 1.0.3
812 * @date 29/01/13
813 *
814 * @param $field_name (string) the field name
815 * @return (mixed)
816 */
817
818 function get_sub_field( $selector = '', $format_value = true ) {
819
820 // get sub field
821 $sub_field = get_sub_field_object( $selector, $format_value );
822
823
824 // bail early if no sub field
825 if( !$sub_field ) return false;
826
827
828 // return
829 return $sub_field['value'];
830
831 }
832
833
834 /*
835 * the_sub_field()
836 *
837 * This function is the same as echo get_sub_field
838 *
839 * @type function
840 * @since 1.0.3
841 * @date 29/01/13
842 *
843 * @param $field_name (string) the field name
844 * @return n/a
845 */
846
847 function the_sub_field( $field_name, $format_value = true ) {
848
849 $value = get_sub_field( $field_name, $format_value );
850
851 if( is_array($value) ) {
852
853 $value = implode(', ',$value);
854
855 }
856
857 echo $value;
858 }
859
860
861 /*
862 * get_sub_field_object()
863 *
864 * This function is used inside a 'has_sub_field' while loop to return a sub field object
865 *
866 * @type function
867 * @since 3.5.8.1
868 * @date 29/01/13
869 *
870 * @param $child_name (string) the field name
871 * @return (array)
872 */
873
874 function get_sub_field_object( $selector, $format_value = true, $load_value = true ) {
875
876 // vars
877 $row = acf_get_loop('active');
878
879
880 // bail early if no row
881 if( !$row ) return false;
882
883
884 // attempt to find sub field
885 $sub_field = get_row_sub_field($selector);
886
887
888 // bail early if no sub field
889 if( !$sub_field ) return false;
890
891
892 // load value
893 if( $load_value ) {
894
895 $sub_field['value'] = get_row_sub_value( $sub_field['key'] );
896
897 }
898
899
900 // format value
901 if( $format_value ) {
902
903 // get value for field
904 $sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field );
905
906 }
907
908
909 // return
910 return $sub_field;
911
912 }
913
914
915 /*
916 * get_row_layout()
917 *
918 * This function will return a string representation of the current row layout within a 'have_rows' loop
919 *
920 * @type function
921 * @since 3.0.6
922 * @date 29/01/13
923 *
924 * @param n/a
925 * @return (string)
926 */
927
928 function get_row_layout() {
929
930 // vars
931 $row = get_row();
932
933
934 // return
935 if( isset($row['acf_fc_layout']) ) {
936
937 return $row['acf_fc_layout'];
938
939 }
940
941
942 // return
943 return false;
944
945 }
946
947
948 /*
949 * acf_shortcode()
950 *
951 * This function is used to add basic shortcode support for the ACF plugin
952 * eg. [acf field="heading" post_id="123" format_value="1"]
953 *
954 * @type function
955 * @since 1.1.1
956 * @date 29/01/13
957 *
958 * @param $field (string) the field name or key
959 * @param $post_id (mixed) the post_id of which the value is saved against
960 * @param $format_value (boolean) whether or not to format the field value
961 * @return (string)
962 */
963
964 function acf_shortcode( $atts ) {
965
966 // extract attributs
967 extract( shortcode_atts( array(
968 'field' => '',
969 'post_id' => false,
970 'format_value' => true
971 ), $atts ) );
972
973
974 // get value and return it
975 $value = get_field( $field, $post_id, $format_value );
976
977
978 // array
979 if( is_array($value) ) {
980
981 $value = @implode( ', ', $value );
982
983 }
984
985
986 // return
987 return $value;
988
989 }
990
991 add_shortcode('acf', 'acf_shortcode');
992
993
994 /*
995 * update_field()
996 *
997 * This function will update a value in the database
998 *
999 * @type function
1000 * @since 3.1.9
1001 * @date 29/01/13
1002 *
1003 * @param $selector (string) the field name or key
1004 * @param $value (mixed) the value to save in the database
1005 * @param $post_id (mixed) the post_id of which the value is saved against
1006 * @return (boolean)
1007 */
1008
1009 function update_field( $selector, $value, $post_id = false ) {
1010
1011 // filter post_id
1012 $post_id = acf_get_valid_post_id( $post_id );
1013
1014
1015 // get field
1016 $field = acf_maybe_get_field( $selector, $post_id, false );
1017
1018
1019 // create dummy field
1020 if( !$field ) {
1021
1022 $field = acf_get_valid_field(array(
1023 'name' => $selector,
1024 'key' => '',
1025 'type' => '',
1026 ));
1027
1028 }
1029
1030
1031 // save
1032 return acf_update_value( $value, $post_id, $field );
1033
1034 }
1035
1036
1037 /*
1038 * update_sub_field
1039 *
1040 * This function will update a value of a sub field in the database
1041 *
1042 * @type function
1043 * @date 2/04/2014
1044 * @since 5.0.0
1045 *
1046 * @param $selector (mixed) the sub field name or key, or an array of ancestors
1047 * @param $value (mixed) the value to save in the database
1048 * @param $post_id (mixed) the post_id of which the value is saved against
1049 * @return (boolean)
1050 */
1051
1052 function update_sub_field( $selector, $value, $post_id = false ) {
1053
1054 // vars
1055 $sub_field = false;
1056
1057
1058 // get sub field
1059 if( is_array($selector) ) {
1060
1061 $post_id = acf_get_valid_post_id( $post_id );
1062 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1063
1064 } else {
1065
1066 $post_id = acf_get_loop('active', 'post_id');
1067 $sub_field = get_row_sub_field( $selector );
1068
1069 }
1070
1071
1072 // bail early if no sub field
1073 if( !$sub_field ) return false;
1074
1075
1076 // update
1077 return acf_update_value( $value, $post_id, $sub_field );
1078
1079 }
1080
1081
1082 /*
1083 * delete_field()
1084 *
1085 * This function will remove a value from the database
1086 *
1087 * @type function
1088 * @since 3.1.9
1089 * @date 29/01/13
1090 *
1091 * @param $selector (string) the field name or key
1092 * @param $post_id (mixed) the post_id of which the value is saved against
1093 * @return (boolean)
1094 */
1095
1096 function delete_field( $selector, $post_id = false ) {
1097
1098 // filter post_id
1099 $post_id = acf_get_valid_post_id( $post_id );
1100
1101
1102 // get field
1103 $field = acf_maybe_get_field( $selector, $post_id );
1104
1105
1106 // delete
1107 return acf_delete_value( $post_id, $field );
1108
1109 }
1110
1111
1112 /*
1113 * delete_sub_field
1114 *
1115 * This function will delete a value of a sub field in the database
1116 *
1117 * @type function
1118 * @date 2/04/2014
1119 * @since 5.0.0
1120 *
1121 * @param $selector (mixed) the sub field name or key, or an array of ancestors
1122 * @param $value (mixed) the value to save in the database
1123 * @param $post_id (mixed) the post_id of which the value is saved against
1124 * @return (boolean)
1125 */
1126
1127 function delete_sub_field( $selector, $post_id = false ) {
1128
1129 return update_sub_field( $selector, null, $post_id );
1130
1131 }
1132
1133
1134 /*
1135 * add_row
1136 *
1137 * This function will add a row of data to a field
1138 *
1139 * @type function
1140 * @date 16/10/2015
1141 * @since 5.2.3
1142 *
1143 * @param $selector (string)
1144 * @param $row (array)
1145 * @param $post_id (mixed)
1146 * @return (boolean)
1147 */
1148
1149 function add_row( $selector, $row = false, $post_id = false ) {
1150
1151 // filter post_id
1152 $post_id = acf_get_valid_post_id( $post_id );
1153
1154
1155 // get field
1156 $field = acf_maybe_get_field( $selector, $post_id, false );
1157
1158
1159 // bail early if no field
1160 if( !$field ) return false;
1161
1162
1163 // get raw value
1164 $value = acf_get_value( $post_id, $field );
1165
1166
1167 // ensure array
1168 $value = acf_get_array($value);
1169
1170
1171 // append
1172 $value[] = $row;
1173
1174
1175 // update value
1176 acf_update_value( $value, $post_id, $field );
1177
1178
1179 // return
1180 return count($value);
1181
1182 }
1183
1184
1185 /*
1186 * add_sub_row
1187 *
1188 * This function will add a row of data to a field
1189 *
1190 * @type function
1191 * @date 16/10/2015
1192 * @since 5.2.3
1193 *
1194 * @param $selector (string)
1195 * @param $row (array)
1196 * @param $post_id (mixed)
1197 * @return (boolean)
1198 */
1199
1200 function add_sub_row( $selector, $row = false, $post_id = false ) {
1201
1202 // vars
1203 $sub_field = false;
1204
1205
1206 // get sub field
1207 if( is_array($selector) ) {
1208
1209 $post_id = acf_get_valid_post_id( $post_id );
1210 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1211
1212 } else {
1213
1214 $post_id = acf_get_loop('active', 'post_id');
1215 $sub_field = get_row_sub_field( $selector );
1216
1217 }
1218
1219
1220 // bail early if no sub field
1221 if( !$sub_field ) return false;
1222
1223
1224 // get raw value
1225 $value = acf_get_value( $post_id, $sub_field );
1226
1227
1228 // ensure array
1229 $value = acf_get_array( $value );
1230
1231
1232 // append
1233 $value[] = $row;
1234
1235
1236 // update
1237 acf_update_value( $value, $post_id, $sub_field );
1238
1239
1240 // return
1241 return count($value);
1242
1243 }
1244
1245
1246 /*
1247 * update_row
1248 *
1249 * This function will update a row of data to a field
1250 *
1251 * @type function
1252 * @date 19/10/2015
1253 * @since 5.2.3
1254 *
1255 * @param $selector (string)
1256 * @param $i (int)
1257 * @param $row (array)
1258 * @param $post_id (mixed)
1259 * @return (boolean)
1260 */
1261
1262 function update_row( $selector, $i = 1, $row = false, $post_id = false ) {
1263
1264 // vars
1265 $offset = acf_get_setting('row_index_offset');
1266 $i = $i - $offset;
1267
1268
1269 // filter post_id
1270 $post_id = acf_get_valid_post_id( $post_id );
1271
1272
1273 // get field
1274 $field = acf_maybe_get_field( $selector, $post_id, false );
1275
1276
1277 // bail early if no field
1278 if( !$field ) return false;
1279
1280
1281 // get raw value
1282 $value = acf_get_value( $post_id, $field );
1283
1284
1285 // ensure array
1286 $value = acf_get_array($value);
1287
1288
1289 // update
1290 $value[ $i ] = $row;
1291
1292
1293 // update value
1294 acf_update_value( $value, $post_id, $field );
1295
1296
1297 // return
1298 return true;
1299
1300 }
1301
1302
1303 /*
1304 * update_sub_row
1305 *
1306 * This function will add a row of data to a field
1307 *
1308 * @type function
1309 * @date 16/10/2015
1310 * @since 5.2.3
1311 *
1312 * @param $selector (string)
1313 * @param $row (array)
1314 * @param $post_id (mixed)
1315 * @return (boolean)
1316 */
1317
1318 function update_sub_row( $selector, $i = 1, $row = false, $post_id = false ) {
1319
1320 // vars
1321 $sub_field = false;
1322 $offset = acf_get_setting('row_index_offset');
1323 $i = $i - $offset;
1324
1325
1326 // get sub field
1327 if( is_array($selector) ) {
1328
1329 $post_id = acf_get_valid_post_id( $post_id );
1330 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1331
1332 } else {
1333
1334 $post_id = acf_get_loop('active', 'post_id');
1335 $sub_field = get_row_sub_field( $selector );
1336
1337 }
1338
1339
1340 // bail early if no sub field
1341 if( !$sub_field ) return false;
1342
1343
1344 // get raw value
1345 $value = acf_get_value( $post_id, $sub_field );
1346
1347
1348 // ensure array
1349 $value = acf_get_array( $value );
1350
1351
1352 // append
1353 $value[ $i ] = $row;
1354
1355
1356 // update
1357 acf_update_value( $value, $post_id, $sub_field );
1358
1359
1360 // return
1361 return true;
1362
1363 }
1364
1365
1366 /*
1367 * delete_row
1368 *
1369 * This function will delete a row of data from a field
1370 *
1371 * @type function
1372 * @date 19/10/2015
1373 * @since 5.2.3
1374 *
1375 * @param $selector (string)
1376 * @param $i (int)
1377 * @param $post_id (mixed)
1378 * @return (boolean)
1379 */
1380
1381 function delete_row( $selector, $i = 1, $post_id = false ) {
1382
1383 // vars
1384 $offset = acf_get_setting('row_index_offset');
1385 $i = $i - $offset;
1386
1387
1388 // filter post_id
1389 $post_id = acf_get_valid_post_id( $post_id );
1390
1391
1392 // get field
1393 $field = acf_maybe_get_field( $selector, $post_id );
1394
1395
1396 // bail early if no field
1397 if( !$field ) return false;
1398
1399
1400 // get value
1401 $value = acf_get_value( $post_id, $field );
1402
1403
1404 // ensure array
1405 $value = acf_get_array($value);
1406
1407
1408 // bail early if index doesn't exist
1409 if( !isset($value[ $i ]) ) return false;
1410
1411
1412 // unset
1413 unset( $value[ $i ] );
1414
1415
1416 // update
1417 acf_update_value( $value, $post_id, $field );
1418
1419
1420 // return
1421 return true;
1422
1423 }
1424
1425
1426 /*
1427 * delete_sub_row
1428 *
1429 * This function will add a row of data to a field
1430 *
1431 * @type function
1432 * @date 16/10/2015
1433 * @since 5.2.3
1434 *
1435 * @param $selector (string)
1436 * @param $row (array)
1437 * @param $post_id (mixed)
1438 * @return (boolean)
1439 */
1440
1441 function delete_sub_row( $selector, $i = 1, $post_id = false ) {
1442
1443 // vars
1444 $sub_field = false;
1445 $offset = acf_get_setting('row_index_offset');
1446 $i = $i - $offset;
1447
1448
1449 // get sub field
1450 if( is_array($selector) ) {
1451
1452 $post_id = acf_get_valid_post_id( $post_id );
1453 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1454
1455 } else {
1456
1457 $post_id = acf_get_loop('active', 'post_id');
1458 $sub_field = get_row_sub_field( $selector );
1459
1460 }
1461
1462
1463 // bail early if no sub field
1464 if( !$sub_field ) return false;
1465
1466
1467 // get raw value
1468 $value = acf_get_value( $post_id, $sub_field );
1469
1470
1471 // ensure array
1472 $value = acf_get_array( $value );
1473
1474
1475 // bail early if index doesn't exist
1476 if( !isset($value[ $i ]) ) return false;
1477
1478
1479 // append
1480 unset( $value[ $i ] );
1481
1482
1483 // update
1484 acf_update_value( $value, $post_id, $sub_field );
1485
1486
1487 // return
1488 return true;
1489
1490 }
1491
1492
1493 /*
1494 * Depreceated Functions
1495 *
1496 * These functions are outdated
1497 *
1498 * @type function
1499 * @date 4/03/2014
1500 * @since 1.0.0
1501 *
1502 * @param n/a
1503 * @return n/a
1504 */
1505
1506 function create_field( $field ) {
1507
1508 acf_render_field( $field );
1509
1510 }
1511
1512 function render_field( $field ) {
1513
1514 acf_render_field( $field );
1515
1516 }
1517
1518 function reset_the_repeater_field() {
1519
1520 return reset_rows();
1521
1522 }
1523
1524 function the_repeater_field( $field_name, $post_id = false ) {
1525
1526 return has_sub_field( $field_name, $post_id );
1527
1528 }
1529
1530 function the_flexible_field( $field_name, $post_id = false ) {
1531
1532 return has_sub_field( $field_name, $post_id );
1533
1534 }
1535
1536 function acf_filter_post_id( $post_id ) {
1537
1538 return acf_get_valid_post_id( $post_id );
1539
1540 }
1541
1542 ?>
1543