acf_form

Acf_form().

Description

acf_form( (array) $options = array() ); 

This function is used to create an ACF form.

Parameters (1)

0. $options — Optional. (array) => array()
The options.

Usage

  1. if ( !function_exists( 'acf_form' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'advanced-custom-fields/core/api.php'; 
  3.  
  4. // The options. 
  5. $options = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = acf_form($options); 
  9.  

Defined (1)

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

/core/api.php  
  1. function acf_form( $options = array() ) 
  2. global $post; 
  3.  
  4.  
  5. // defaults 
  6. $defaults = array( 
  7. 'post_id' => false,  
  8. 'field_groups' => array(),  
  9. 'form' => true,  
  10. 'form_attributes' => array( 
  11. 'id' => 'post',  
  12. 'class' => '',  
  13. 'action' => '',  
  14. 'method' => 'post',  
  15. ),  
  16. 'return' => add_query_arg( 'updated', 'true', get_permalink() ),  
  17. 'html_before_fields' => '',  
  18. 'html_after_fields' => '',  
  19. 'submit_value' => __("Update", 'acf'),  
  20. 'updated_message' => __("Post updated", 'acf'),  
  21. ); 
  22.  
  23.  
  24. // merge defaults with options 
  25. $options = array_merge($defaults, $options); 
  26.  
  27.  
  28. // merge sub arrays 
  29. foreach( $options as $k => $v ) 
  30. if( is_array($v) ) 
  31. $options[ $k ] = array_merge($defaults[ $k ], $options[ $k ]); 
  32.  
  33.  
  34. // filter post_id 
  35. $options['post_id'] = apply_filters('acf/get_post_id', $options['post_id'] ); 
  36.  
  37.  
  38. // attributes 
  39. $options['form_attributes']['class'] .= 'acf-form'; 
  40.  
  41.  
  42.  
  43. // register post box 
  44. if( empty($options['field_groups']) ) 
  45. // get field groups 
  46. $filter = array( 
  47. 'post_id' => $options['post_id'] 
  48. ); 
  49.  
  50.  
  51. if( strpos($options['post_id'], 'user_') !== false ) 
  52. $user_id = str_replace('user_', '', $options['post_id']); 
  53. $filter = array( 
  54. 'ef_user' => $user_id 
  55. ); 
  56. elseif( strpos($options['post_id'], 'taxonomy_') !== false ) 
  57. $taxonomy_id = str_replace('taxonomy_', '', $options['post_id']); 
  58. $filter = array( 
  59. 'ef_taxonomy' => $taxonomy_id 
  60. ); 
  61.  
  62.  
  63. $options['field_groups'] = array(); 
  64. $options['field_groups'] = apply_filters( 'acf/location/match_field_groups', $options['field_groups'], $filter ); 
  65.  
  66.  
  67. // updated message 
  68. if(isset($_GET['updated']) && $_GET['updated'] == 'true' && $options['updated_message']) 
  69. echo '<div id="message" class="updated"><p>' . $options['updated_message'] . '</p></div>'; 
  70.  
  71.  
  72. // display form 
  73. if( $options['form'] ): ?> 
  74. <form <?php if($options['form_attributes']) {foreach($options['form_attributes'] as $k => $v) {echo $k . '="' . $v .'" '; }} ?>> 
  75. <?php endif; ?> 
  76.  
  77. <div style="display:none"> 
  78. <script type="text/javascript"> 
  79. acf.o.post_id = <?php echo is_numeric($options['post_id']) ? $options['post_id'] : '"' . $options['post_id'] . '"'; ?>; 
  80. </script> 
  81. <input type="hidden" name="acf_nonce" value="<?php echo wp_create_nonce( 'input' ); ?>" /> 
  82. <input type="hidden" name="post_id" value="<?php echo $options['post_id']; ?>" /> 
  83. <input type="hidden" name="return" value="<?php echo $options['return']; ?>" /> 
  84. <?php wp_editor('', 'acf_settings'); ?> 
  85. </div> 
  86.  
  87. <div id="poststuff"> 
  88. <?php 
  89.  
  90. // html before fields 
  91. echo $options['html_before_fields']; 
  92.  
  93.  
  94. $acfs = apply_filters('acf/get_field_groups', array()); 
  95.  
  96. if( is_array($acfs) ) { foreach( $acfs as $acf ) { 
  97.  
  98. // only add the chosen field groups 
  99. if( !in_array( $acf['id'], $options['field_groups'] ) ) 
  100. continue; 
  101.  
  102.  
  103. // load options 
  104. $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']); 
  105.  
  106.  
  107. // load fields 
  108. $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']); 
  109.  
  110.  
  111. echo '<div id="acf_' . $acf['id'] . '" class="postbox acf_postbox ' . $acf['options']['layout'] . '">'; 
  112. echo '<h3 class="hndle"><span>' . $acf['title'] . '</span></h3>'; 
  113. echo '<div class="inside">'; 
  114.  
  115. do_action('acf/create_fields', $fields, $options['post_id']); 
  116.  
  117. echo '</div></div>'; 
  118.  
  119. }} 
  120.  
  121.  
  122. // html after fields 
  123. echo $options['html_after_fields']; 
  124.  
  125. ?> 
  126.  
  127. <?php if( $options['form'] ): ?> 
  128. <!-- Submit --> 
  129. <div class="field"> 
  130. <input type="submit" value="<?php echo $options['submit_value']; ?>" /> 
  131. </div> 
  132. <!-- / Submit --> 
  133. <?php endif; ?> 
  134.  
  135. </div><!-- <div id="poststuff"> --> 
  136.  
  137. <?php if( $options['form'] ): ?> 
  138. </form> 
  139. <?php endif;