Search code examples
phpninja-forms

Adding $timestamp to Multiple Ninja Form submissions


I have managed to add a $timestamp to a single Ninja Form submission, but not sure how to expand the below code so will work on more than one form.

This is the code that works for a single Ninja Form

<?php
/*
Plugin Name: Time Stamp
*/
function my_ninja_forms_date_code(){
    //Declare $ninja_forms_processing as a global variable.
    global $ninja_forms_processing;
    //only process this code on the form ID 1
    $form_id = $ninja_forms_processing->get_form_ID();
    if( $form_id == 2 ){
        //sets timestamp variable to current time
        $timestamp = date('G:i:s');
        //Update the hidden field value for the field with an ID of 41 to the
current time.
        $ninja_forms_processing->update_field_value( 41, $timestamp );
    }
}
add_action( 'ninja_forms_process', 'my_ninja_forms_date_code' );
?>

I have tried to add an elseif condition for two forms but was not accepted, see code below:

<?php
/*
Plugin Name: Example Plugin
*/
<?php
function my_ninja_forms_date_code(){
    //Declare $ninja_forms_processing as a global variable.
    global $ninja_forms_processing;
    //only process this code on the form ID 1
    $form_id = $ninja_forms_processing->get_form_ID();

    if( $form_id == 2 ){
        //sets timestamp variable to current time
        $timestamp = date('G:i:s');
        //Update the hidden field value for the field with an ID of 3 to the current time.
        $ninja_forms_processing->update_field_value( 41, $timestamp );
    }
    elseif ( $form_id == 6 ){
        //sets timestamp variable to current time
        $timestamp = date('G:i:s');
        //Update the hidden field value for the field with an ID of 43 to the current time.
        $ninja_forms_processing->update_field_value( 43, $timestamp );
    }
}
add_action( 'ninja_forms_process', 'my_ninja_forms_date_code' );
?>

The $form_id refers to the Ninja Form ID no and the 41 in the $ninja_forms_processing->update_field_value( 41, $timestamp ); is taken from the hidden Field id no.

Any suggestions/guidance would be most welcome.


Solution

  • There are two <?php opening tags in the file you mentioned just erase the second one and your code would do just fine without an error.