Hi @mukape,
Hope this message finds you well.
Our Second Line Support was able to provide this mu-plugin:
add_action( 'wp_footer', 'wpmudev_start_form_calculate', 9999 );
function wpmudev_start_form_calculate() {
global $post;
if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'forminator_form' ) ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
setTimeout(function() {
$('.forminator-custom-form').trigger('after.load.forminator');
},100);
$(document).on('after.load.forminator', function( e, form_id ) {
if ( e.target.id == 'forminator-module-6' ) { // Please change 6 to your form's ID.
$(document).on('click', '.forminator-button-next', function(){
var date_filled = $('input[name="hidden-1"]').val();
if ( ! date_filled ) {
var date_start = new Date();
var time = date_start.getTime();
$('input[name="hidden-1"]').val(time);
}
});
$(document).on('click', '#forminator-submit', function(){
var date_end = new Date();
var time_end = date_end.getTime();
$('input[name="hidden-2"]').val(time_end);
var diff = time_end - $('input[name="hidden-1"]').val();
$('input[name="hidden-3"]').val( millisToMinutesAndSeconds(diff) );
});
}
});
function millisToMinutesAndSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
});
</script>
<?php
}
add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val', 10, 2 );
function wpmudev_update_hidden_field_val( $prepared_data, $module_object ) {
$form_ids = array(6); // Please change the form ID.
if ( !in_array( $module_object->id, $form_ids ) ) {
return $prepared_data;
}
foreach ( $prepared_data as $key => $value ) {
if ( strpos( $key, 'hidden-1' ) !== false ) {
$prepared_data[$key] = sanitize_text_field( $_POST[$key] );
}
if ( strpos( $key, 'hidden-2' ) !== false ) {
$prepared_data[$key] = sanitize_text_field( $_POST[$key] );
}
if ( strpos( $key, 'hidden-3' ) !== false ) {
$prepared_data[$key] = sanitize_text_field( $_POST[$key] );
}
}
return $prepared_data;
}
add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data', 10, 3 );
function wpmudev_change_hidden_field_data( $entry, $module_id, $field_data_array ) {
$form_ids = array(6); // Please change the form ID.
if ( ! in_array( $module_id, $form_ids ) ) {
return;
}
foreach ( $field_data_array as $key => $value ) {
if ( strpos( $value['name'], 'hidden-1' ) !== false ) {
Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
}
if ( strpos( $value['name'], 'hidden-2' ) !== false ) {
Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
}
if ( strpos( $value['name'], 'hidden-3' ) !== false ) {
Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
}
}
}
Note that you need to update your form ID on this line (replacing 6):
if ( e.target.id == 'forminator-module-6' )
You will find how to install a mu-plugin on this link https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins. The first step is only a Next button (changed text to Start), then add three hidden fields with custom values (start time, end time, total time).
You will find a test form on this link: https://drive.google.com/file/d/1OyeOeLosAdzDpVb56l54aQ-UJtQAmGKa/view?usp=sharing
Let us know if you require additional assistance.
Best regards,
Laura