I have a form that contains a select menu that is being dynamically populated using custom fields from repeater rows. The dynamic population of the select menu totally works. However, I then want the selection to be tied to a different email address (a custom field in the same repeater) so that the selection determines which email address to send a notification of the form submission.
The repeater name is partners
and each row has the following fields:
partners_country
, for the dynamic population
partners_email
, for the email address that should be tied to that select option and used to send the notification
Below I am going to share the code I’ve used for the dynamic population, which does work. And then the code for the notification filter, which does not work . I am looking for some assistance to make that second block of code do what I want it to do.
What that second block of code is doing is that it it connecting to ACF in that it will send a notification but it’s using the email of that very last row, regardless of which option has been selected in the select menu.
I feel like I’m a few lines of code from this working, but the issue is using $entry to somehow tie the selection to the email address through an array or something.
If it helps, I was trying to reference this code, where someone is doing this based on WP users, rather than ACF fields:
Dynamic Population Code (working)
// Populate GF with ACF Repeater
add_filter('gform_pre_render_1', 'dynamic_populate'); // Where 1 is GF ID
function dynamic_populate($form) {
foreach($form['fields'] as &$field){
if(strpos($field['cssClass'], 'region-field') === false)
continue;
global $post;
$id = $post->ID;
global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'partners_%_partners_country'
// repeater-name_%_field-name
));
$choices = array(array('text' => 'Select a Region', 'value' => ' '));
if($rows){
foreach($rows as $row) {
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'partners_' . $matches[0] . '_partners_country';
$valueText = get_post_meta($post->ID,$meta_key,true);
$value = str_replace(' ', '-', strtolower($valueText));
$choices[] = array('text' => $valueText, 'value' => $value);
}
}
$field->choices = $choices;
}
return $form;
}
Notification Filter Code (not working)
add_filter('gform_notification_1', 'contact_email_notification', 10, 3);
function contact_email_notification($notification, $form , $entry) {
foreach($form['fields'] as &$field) {
if($field['type'] != 'select' || strpos($field['cssClass'], 'region-field') === false )
continue;
global $post;
$id = $post->ID;
global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE post_id = %d AND meta_key LIKE %s
",
$id,
'partners_%_partners_email'
// repeater-name_%_field-name
));
// $field_id = (string) $field['id'];
// $post_id = $entry[ $field['id']];
if($rows){
foreach($rows as $row) {
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'partners_' . $matches[0] . '_partners_email';
$email_to = get_post_meta($post->ID,$meta_key,true);
}
}
}
if (!empty($email_to)) {
$notification['to'] = $email_to;
}
return $notification;
}