Support

Account

Home Forums Front-end Issues Processing multiple acf_form of same type

Solved

Processing multiple acf_form of same type

  • So I’ve altered WP’s menu editor to include an acf_form in each menu item. By setting 'form' => false I’m able to process the whole screen when clicking “save menu”. Of course the downside is that only the last instance of acf_form actually gets processed.

    So in the case where I have something like this:

    
    foreach ( $items as $item ) {
      $acf_form( array( 'post_id' => $item->ID, 'field_groups' => array('my_group'), 'form' => false ) );
    }
    

    Would there be a simple way to get each post to update respectively when the entire page is submitted?

    If not, how would I force $_POST to have an array of unique acf forms so that I can process manually?

    Thanks!

  • Hi @joelstransky

    I’m afraid you need to modify the core files to do that. Another easier method would be using Javascript to modify the name of the input so it’s changed from acf[field_1234567890abc] to acf[field_1234567890abc][]. That way the data would have a unique identifier.

    I hope this helps 🙂

  • @James,
    Thanks for the suggestion. I actually came up with a slightly more elegant solution.
    First I pluralize the name attribute by hooking into 'acf/prepare_field'

    
    // My field group contains only a single Post Object field
    add_filter('acf/prepare_field/type=post_object', 'my_acf_prepare_field' );
    function my_acf_prepare_field ( $field ) {
        $field['_input'] = $field['_input'] . '[]';
        return $field;
    }
    

    That will create an array of entries in $_POST

    
    [acf] => Array
            (
                [field_573e727f24ef4] => Array
                    (
                        [0] => 170
                        [1] => 152
                        [2] => 155
                        [3] => 170
                    )
    
                [_validate_email] =>
            )
    

    This creates 2 issues however.
    1. The array keys are kinda meaningless to me.
    2. acf_form_head() cannot process this. IMO it should be able to in a future version.

    To fix the first issue I had to store a post id in $GLOBALS at the time I was calling acf_form()

    
    // leaving out some context here but this demonstrates the idea
    $GLOBALS['current_menu_item_id'] = $item_id;
    $acf_options = array(
      'post_id' => $item_id,
      'form' => false,
      'field_groups' => array('group_573e71ff44db1'),
      'return' => '',
    );
    acf_form( $acf_options );
    

    Now we can modify our earlier prepare_field handler to pluraize with meaning

    
    function my_acf_prepare_field ( $field ) {
      $field['_input'] = $field['_input'] . '[' . $GLOBALS['current_menu_item_id'] . ']';
      return $field;
    }
    

    Which creates something in $_POST we can use during our fix for the second issue.

    
    [acf] => Array
      (
        [field_573e727f24ef4] => Array
          (
            [172] => 170
            [173] => 152
            [174] => 155
            [175] => 170
          )
    
        [_validate_email] =>
    )
    

    To fix the second issue I removed acf_form_head() and called acf’s meta update manually. In my case I was saving changes to a Menu so I just hooked into the relevant filter but this could be done wherever you’re handling your form submit. This is inspired by Remi Corson

    
    // save menu custom fields
    add_action( 'wp_update_nav_menu_item', 'my_update_custom_nav_fields', 10, 3 );
    function my_update_custom_nav_fields ($menu_id, $menu_item_db_id, $args) {
      if ( is_array( $_REQUEST['acf']['field_573e727f24ef4'] ) ) {
        $field_value = $_REQUEST['acf']['field_573e727f24ef4'][$menu_item_db_id];
        $field = get_field_object('field_573e727f24ef4', $menu_item_db_id, false, false);
        acf_update_value( $field_value, $menu_item_db_id, $field );
      }
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Processing multiple acf_form of same type’ is closed to new replies.