Search code examples
phpwordpressninja-forms

Trouble with Ninja Forms ninja_forms_submit_data hook


Could someone tell me what am I doing wrong with this Ninja Forms hook:

add_filter( 'ninja_forms_submit_data', 'my_ninja_forms_submit_data' );

function my_ninja_forms_submit_data( $form_data ) {
    foreach( $form_data[ 'fields' ] as $field ) { 
        if( 'test_page_url_1519171605789' == $field['key'] ){
            $current_url =  "my url - {$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
            $field[ 'value' ] = $current_url; 
        }
    }
    $form_settings = $form_data[ 'settings' ]; // Form settings.
    $extra_data = $form_data[ 'extra' ]; // Extra data included with the submission.

    return $form_data;
}

I am trying to modify the hidden form field with the key value "test_page_url_1519171605789" so that it contains a URL.


Solution

  • I was able to solve this issue by using this code instead:

    add_filter( 'ninja_forms_submit_data', 'my_ninja_forms_submit_data' );
    
    function my_ninja_forms_submit_data( $form_data ) { 
    
        //Need to set the current URL as the previous page since
        //REQUEST_URI was returning /wp-admin/admin-ajax.php 
        //instead of the form's actual URL.
        $current_url =  $_SERVER['HTTP_REFERER'];
    
        foreach( $form_data[ 'fields' ] as $key => $field ) {
            //I need to look for the field ID and not the field key
            if( $key == '197' || $key == '195' || $key == '196' || $key == '179' ){
                // Update the submitted field value with the URL to the previous page.
                $form_data['fields'][$key]['value'] = $current_url; 
          }
      }
    
      // Form settings.
      $form_settings = $form_data[ 'settings' ]; 
      // Extra data included with the submission.
      $extra_data = $form_data[ 'extra' ]; 
    
      return $form_data;
    }