Search code examples
phpwordpressninja-forms

Pass a variable to a Ninja Form field


THE AIM
The following function should pre-populate a hidden field (hidden_email...) on my form using a variable.

THE SUSPICION
The variable "$cust_email" that I need to pass to the field is however declared already while the page is being loaded. And at this moment it contains an email address.

CURRENT RESULT
The problem is, that when this code below runs, the variable seems to be already empty. Nothing is being passed to the form's field.

function create_page() {

    $cust_email = $json->emailAddress;


function ninja_forms_handler() {
    add_action ( 'ninja_forms_pre_process', 'add_customers_email' );
}
add_action( 'init', 'ninja_forms_handler' );

function add_customers_email() {
    global $ninja_forms_processing; 

    $form_id = $ninja_forms_processing->get_form_ID();  
    if( $form_id == 3 ){     
        $ninja_forms_processing->update_field_settings( ‘hidden_email_1488454108179’, $cust_email );
    }     
  }
}

Solution

  • How to update or fill or pre-populate with default value any Ninja form field from WordPress Post Meta Value:

    add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
    function wm_the_value( $default_value, $field_type, $field_settings ) {
        if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
      $default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
      }
    
      return $default_value;
    }
    

    ref is field name in Ninja form.

    _listing_mls is meta_key name from WP database for post meta field.