Thread Starter
msslrb
(@msslrb)
Sorry, I accidentally eft the important part out. I’m not sure if I’m using deprecated hooks or if it’s an address issue.
// 1. Capture the Entry ID
add_action( 'forminator_custom_form_submit_before_set_fields', function( $entry, $form_id, $field_data_array ) {
$_POST['actual_submission_id'] = $entry->entry_id;
}, 20, 3 );
// 2. Hook into submission
add_action('forminator_form_after_save_entry', 'process_petition_submission', 10, 2);
add_action('forminator_form_after_handle_submit', 'process_petition_submission', 10, 2);
function process_petition_submission($form_id, $response) {
$entry_id = isset($_POST['actual_submission_id']) ? absint($_POST['actual_submission_id']) : 0;
if (!$entry_id && isset($response['entry_id'])) $entry_id = absint($response['entry_id']);
if (!$entry_id) return;
global $wpdb;
if (!class_exists('Forminator_API')) return;
$entry = Forminator_API::get_entry((int)$form_id, (int)$entry_id);
if (is_wp_error($entry) || !$entry) return;
$data = [];
foreach ($entry->meta_data as $field) {
$slug = $field['name'] ?? '';
$value = $field['value'] ?? '';
if (!$slug) continue;
if (is_array($value)) {
$value = implode(', ', array_filter($value));
}
$data[$slug] = $value;
}
// INSERT INTO MAIN PETITIONS TABLE
$wpdb->insert(
$wpdb->prefix . 'petitions',
[
'form_id' => $form_id,
'submission_id' => $entry_id,
'first_name' => sanitize_text_field($data['first_name'] ?? ''),
'last_name' => sanitize_text_field($data['last_name'] ?? ''),
'credentials' => sanitize_text_field($data['credentials'] ?? ''),
'email' => sanitize_email($data['email'] ?? ''),
'phone' => sanitize_text_field($data['phone'] ?? ''),
'p_name' => sanitize_text_field($data['p_name'] ?? ''),
'p_address' => sanitize_text_field($data['p_address'] ?? ''),
'created_at' => current_time('mysql')
]
);
$petition_row_id = $wpdb->insert_id;
// INSERT INTO CONCERNS TABLE
if ($petition_row_id) {
$concerns_table = $wpdb->prefix . 'petition_concerns';
foreach ($data as $key => $value) {
if (strpos($key, 'concern_') === 0 && !empty($value)) {
$wpdb->insert($concerns_table, [
'petition_id' => $petition_row_id,
'concern_group' => $key,
'concern_value' => sanitize_text_field($value),
'created_at' => current_time('mysql')
]);
}
}
}
// 3. Handle Transient for redirect pages
$petitions = get_posts([
'post_type' => 'petition',
'numberposts' => 1,
'meta_query' => [['key' => '_form_id', 'value' => $form_id]]
]);
if (!empty($petitions)) {
$petition_post_id = $petitions[0]->ID;
set_transient('petition_last_entry_' . $petition_post_id, $entry_id, 10 * MINUTE_IN_SECONDS);
}
}
Hi @msslrb,
Trust you are doing well, and thank you for reaching out to us.
I have pinged our development team to check if the hooks used in your code are valid, and we’ll update you here once we have more feedback on this as soon as possible.
However, please note that custom codes are out of the scope of the general support we provide here. You need to hire a developer if it is required to validate the code.
That being said, can you please also share an export of the form so that we can take a closer look at this and see if we can help you with some feedback? Please share the form export using Google Drive or Pastebin.com.
I hope the following guide comes in handy: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export
Looking forward to hearing back from you.
Best Regards,
Nebu John
Hi @msslrb,
We have a confirmation from our development team, the hooks that have been used in your code are valid.
Please share an export of the form so that we can take a look at this further and see if we can share some insights.
Best Regards,
Nebu John
Thread Starter
msslrb
(@msslrb)
Hi @msslrb,
In the given code, the petition_install_tables() function has a nested function definition and never actually runs dbDelta(). Is that expected?
Also, foreach ($entry->meta_data as $field) is using the wrong structure; in Forminator, $entry->meta_data is almost always an associative array.
Please do note that providing custom code is outside our support scope. For that, you’ll need to hire a developer to provide the required custom code for you. WordPress provides a jobs directory here https://jobs.wordpress.net/
If you need further advice about it, feel free to email us at wpsupport@incsub.com using the following subject.
“Subject: ATTN: WPMU DEV support – wp.org”
Regards,
Nithin