Home › Forums › Front-end Issues › Processing multiple acf_form of same type › Reply To: Processing multiple acf_form of same type
@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 );
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.