Home › Forums › Front-end Issues › 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!
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 );
}
}
You must be logged in to reply to this topic.
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.