Support

Account

Home Forums ACF PRO Prefill for acf_form?

Solving

Prefill for acf_form?

  • Hi, I’m using acf_form() so that I can use it with a custom table. However it looks like acf_form() kinda expects it to map back to a WordPress Post. Is there a way to prefill the data?

    eg:

    
    <?php acf_form([
        'id' => 'updateForm',
        'new_post' => true,
        'data' => ['field_key' => 'My Value', 'my_input_field' => 'My Response'],
        'submit_value' => __("Add", 'acf'),
        'field_groups' => [$acfID)]
    ]); ?>
    

    Thanks!

  • There isn’t anything in acf_form() that will let you fill fields other than the default value setting for the field or you can use acf/load_value filters for each of the fields https://www.advancedcustomfields.com/resources/acfload_value/

  • from the documentation:

    This hook allows you to modify the value of a field right after it is loaded from the database.

    It has a $post_id – the post id which the value was loaded from

    I don’t see how this would work in a front end form – where the content is brand new?

  • The load value filter will run on the field even if it does not have a value already saved. The value will be NULL, or some other empty value. It would be safest to use the field key to set up your filter because the filter may not run using the field name with a non-existent value.

  • The value will be NULL, or some other empty value

    I still don’t get it. If the value would be null/empty, how could one fill it with custom content?


    @phillip
    I also needed to pre-fill a front-end form and I cheated with jQuery.

  • 
    add_filter('acf/load_value/key=field_12345', 'load_my_field_12345', 20, 3);
    function load_my_field_12345($value, $post_id, $field) {
      // check for empty value
      if ($value !== NULL) {
        // you may need to check for something else specific to this field type
        // but not being empty indicates a value was already set for this post
        // so bail
        return $value;
      }
      $value = "whatever value is appropriate for this field type":
      return $value;
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Prefill for acf_form?’ is closed to new replies.