Support

Account

Home Forums Front-end Issues acf_form to retrieve nested fields

Solved

acf_form to retrieve nested fields

  • Hello all. I could really use a bit of guidance regards using acf_form.

    Currently, im working on a site that uses quite a complex field on all the pages to allow the client plenty of flexibility. The field contains repeaters and flexible content.

    I wanted to attempt to add the functionality to find existing wysiwyg editor sub fields, and add front end editing for those fields using acf_form.

    So far, inside the have_rows loop, I have the following snippet of code:
    _____________________________
    $field = ‘editor’; // ‘editor’ being the field name for a wysiwyg editor

    $fkey = get_sub_field_object($field); // retrieve the key for the editor

    acf_form(array( ‘fields’ => array($fkey[‘key’]) )); // hopefully display editor & contents
    _____________________________

    This places a wysiwyg editor on the front end for each field that has an editor. Trouble is, the field is empty instead of containing the actual contents of that field.

    Im not a PHP jedi in any sense, so Im probably misunderstanding how acf_form works. If anybody could help me understand, I would appreciate it.

    Thanks very much for your time

  • What version of ACF are you using? I just did a test and when I include a wysiwyg field in acf_form() that is a sub field of a repeater and the content added in the back end is shown in the front end.

    Or it may be because you’re trying to show the form in the loop that’s getting the field keys. Try storing all the field keys in an array and then use a single acf_form() call to display all of the fields.

  • Thanks John.

    I’m using the latest version of pro.

    I was calling acf_form outside the loop as far as im aware. Its a strange one. If I call acf_form() with an empty array, it displays the the fields on the page with a the contents correctly. But when I try and pick out the wysiwyg specifically, the form displays the editor but its contents are empty.

    Having said that, there was an important bit of info I forgot to provide: Im running wordpress on a localhost using wamp. Does this have any bearing on how acf_form works?

  • The server shouldn’t effect it.

    You posted this

    
    $field = 'editor'; // 'editor' being the field name for a wysiwyg editor
    
    $fkey = get_sub_field_object($field); // retrieve the key for the editor
    
    acf_form(array( 'fields' => array($fkey['key']) )); // hopefully display editor & contents
    

    This makes me believe that you looping through the rows getting the value for $fkey and while still in that loop you’re calling acf_form(). I think you’re going to need to give me some more code to look at.

    Just in case you’re unaware of this, if you put backticks around your code it will make make it look like the code I added above.

  • Thanks John. Only just seen this. Never got an email notification for some reason.

    Anyway, you are right. As im using a repeater with a few wysiwyg editor fields, I am looping through the rows.

    Ok, so saying I have 5 or so repeater editors and im outside the row loop, how do I target a particular editor? They all share the same field key, so I only get the last one in my form.

    Thanks again John.:)

  • Looks like your not the only one not getting notifications, I did not get a notification that you replied. Sorry if this left you hanging.

    anyway. If you wanted to target only the last row of a repeater, or any row. I would to this using get_post_meta() instead of ACF functions.

    
    $repeater = 'repeater_field_name';
    $sub_field = 'sub_field_name';
    $repeater_count = intval(get_post_meta($post_id, $repeater, true);
    if ($repeater_count) {
      $last_row = $repeater_count - 1;
      $value = get_post_meta($post_id, $repeater.'_'.$last_row.'_'.$sub_field, true);
      // $value holds the value of the sub field of the last row
    }
    
  • I’m noticing that each time I reply, I have to re-check the notification icon at the top of this thread. It seems to turn off.

    Anyway, regards your code there. I can use it to fetch the contents of any field in a repeated. But not sure how I would apply it when trying to bring up a front end form for editing that field. Is it used along with acf_form or some other way?

  • I dont know what happened but the thread is showing as solved. I must have done something by accident. Any way to un-solve this please?

  • I must have missed the fact that you wanted to use this with a acf_form().

    There isn’t any way that I know of to only show one row of a repeater field on a front end form. You can specify which fields you want to display but that’s as far as it goes. The front end form would show all the rows.

    If you wanted to use the with a front end form you’d need to create a separate field group for the front end form and then use the acf/pre_save_post action to update the repeater field with the new row.

  • Hi,

    is there by now a way to only show one row of a repeater field?

    My problem is this: I’m trying to build a math quiz for spam protection for a front end form.

    So I’d need to select one random pair of question and answer from a repeater field in an options group, display the question field in the form and then validate the input value against the answer in the specific repeater row.

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

The topic ‘acf_form to retrieve nested fields’ is closed to new replies.