Replies: 2
Hello forminator team,
I have to create a custom post with the content of the form submitted. The form works fine, shows up in the submissions tab, email is sent after submit, but the script is not correct or is not being run.
// Hook into Forminator's form submission
add_action( 'forminator_custom_form_submit_success', 'capture_forminator_form_data', 10, 2 );
function capture_forminator_form_data( $entry_id, $form_id ) {
// Check if the submitted form is the one we're interested in
if ( $form_id != 5861 ) {
return; // Exit if it's not the desired form
}
// Get form data using Forminator's API
$form_data = Forminator_API::get_instance()->get_form_entry( $entry_id );
// Initialize an empty string to store the content
$post_content = '';
// Iterate over form fields to collect values
foreach ( $form_data->fields as $field ) {
// Skip fields that are not submitted or empty
if ( ! isset( $field['value'] ) || empty( $field['value'] ) ) {
continue;
}
// Concatenate field label and value to the post content
$post_content .= $field['field_label'] . ': ' . $field['value'] . "\n";
}
// Create a new post
$post_data = array(
'post_title' => $form_data->title, // Use form title as post title
'post_content' => $post_content, // Use concatenated form field values as post content
'post_type' => 'repair-request', // Custom post type
'post_status' => 'publish',
);
$post_id = wp_insert_post( $post_data );
}
The script lives in the snippets plugin.
Please advice, Sander