I’m struggling with an acf_form that collects a value then passes to a sub field in a repeater field.
Thanks in advance for any suggestions or help. I’m spinning in circles at this point.
I used this acf_form thread as a starting point. Concerned I’ve either over complicated what I need or missed something while customizing for my situation. I originally had have_rows() loop but changed it to what
s below since I want the user to be able to add a rating regardless of if there are rows or not.
The current form renders and partially functions but the value isn’t getting passed on the repeater sub field.
My last attempt was to track by adding error_log() throughout my acf/save_post, but am getting more confused on what/where the issue could be. I tried turning off all plugins except ACF, Oxygen, and my drop-in plugin.
Here are the details:
ACF Pro: 6.2.7
WP: 6.4.3
Theme: Oxygen Builder (actions/hooks are located in a drop-in plugin)
Front End Form (placed on CPT ‘business’ template):
<?php while ( have_posts() ) : the_post(); ?>
<?php acf_form(array(
'post_id' => get_the_ID(),
//'field_groups' => array('group_xxxx'), //previous attempt - field group contains front-end field and repeater with sub field (radio button)
'fields' => array(
'field_yyyy', // front-end field to collect data (radio button)
),
'submit_value' => "Share",
'updated_message' => "Thanks for Sharing!",
'form' => true,
'new_post' => false,
)); ?>
<?php endwhile; ?>
Actions/Hooks located in my drop-in plugin:
Load acf_form_head before get_header
//Add acf_post_type into pages before get_header...
add_action('wp_head', 'site_wide_acf_forms',1);
function site_wide_acf_forms() {
if ( get_post_type() == 'business' ){
acf_form_head();
} // end of IF statement
}
Action to pass value from form field to repeater field
//Hook frontend Form -> then pass value to Repeater
add_action('acf/save_post', 'my_acf_add_emoji', 15); //set to above 10 so acf/save_post grabs updated value
function my_acf_add_emoji( $post_id ) {
// Check the new value of the front end field.
$emoji = get_field('field_yyyy', $post_id);
// **TEMP DEBUG**
error_log("Value Before: " . $emoji);
error_log("Var Export Before: <pre>" . var_export($emoji) . "</pre>");
// Check if specific value was updated
if ( isset($emoji) ) {
// **TEMP DEBUG**
error_log("Var Export Isset: <pre>" . var_export($emoji) . "</pre>");
$max_rows = 1; // Set the maximum number of rows to add
$added_rows = 0;
while( $added_rows < $max_rows ) {
$row = array(
'field_yyyy' => $emoji, //Grab the FrontEnd field value
);
//Add a radio button value (sub field) to the Repeater..
add_sub_row('field_zzzz', $row); //uses field key for parent repeater
$added_rows++;
// **TEMP DEBUG**
error_log("Value Added Row: " . $emoji);
error_log("Var Export Added Row: <pre>" . var_export($emoji) . "</pre>");
}
}
}
FWIW, here’s a sample of the last error logs capture after I submitted the form:
PHP message: Value Before: 8
PHP message: Var Export Before:
.
PHP message: Var Export Isset:
.
PHP message: Value Added Row: 8
PHP message: Var Export Added Row:
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!
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.