Support

Account

Home Forums ACF PRO Best Practices for adding repeater rows via front-end form

Helping

Best Practices for adding repeater rows via front-end form

  • Some background:

    I’m working with a client that wants to have the ability to for customers to create small boards of user-selected products that they are interested in using for an upcoming home re-model (think a extremely basic Pinterest board).

    These boards will just be linked to a single Custom Post that features a repeater row called products featuring several sub_fields with data about said product.

    What I’m looking for:

    I want to be able to create a simple submit button (almost as an “add to cart”) button that posts that products data via some hidden fields to their current board. So I’m trying to find out the best practice for doing this.

    Right now I’ve got it working fine with some hard coded data in functions.php

      
    add_action('acf/save_post', 'my_save_post');
    
    function my_save_post( $post_id ) {
    
      if( get_post_type($post_id) == 'board' ) {
      .....
      ......
          $row = array(
            'name' => 'this is a name'
          );
          //Update the repeater row
          add_row( 'product', $row, $post_id );
      .....
      ......
    }
    
    }    
    

    And using acf_form(); on the frontend template.

    My two questions would be:

    What is the best way to dynamically past that data (‘name’) in this example to that code in functions.php?

    I’ve seen examples through googling of people using 'html_before_fields' => '' to pass additional inputs into the form, but I’m curious to know if there is a better way.

    Also, is there way to use the acf_form() simply only for submitting new data. IE without echoing the current repeater rows and simply just a “submit” button.

    I’ve tried setting field_groups to false via the $options in acf_form(), but this outputs an error and Passing an empty array echoes all the fields.

    A hacky way I’ve got it to remove all the info is passing a field group with a name that does not exist.

  • Hi @rhysm

    I believe the best way would be using the html_before_fields option.

    If you want to show only the submit button, I believe you can add the fields or field_groups options with any ID that doesn’t exist as you have done.

    Hope this helps ๐Ÿ™‚

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.