wordpressninja-forms

Ninja Forms Rendering Values to Hiden Field with get_user_meta


I'm trying to client id for a third part application where I have to use Zapier to file sync, this is separate from the user id created with WordPress. I need to create a specific client id based on the first name of a form. So for example: client-123.

I'm getting my form field values from my Ninja Forms like so:


add_action( 'eks_ninja_forms_processing', 'my_ninja_forms_processing_callback');

function my_ninja_forms_processing_callback( $form_data ){

    $user_id = get_current_user_id();
    $form_id = $form_data[ 'form_id' ];
    $form_fields = $form_data[ 'fields' ];

    foreach( $form_fields as $field ){
        $field_id    = $field[ 'id' ];
        $field_key   = $field[ 'key' ];
        $field_value = $field[ 'value' ];
        if( 'firstname_1603293500271' == $field[ 'key' ] )
            update_user_meta( $user_id, 'child_name', $field_value );   
        }
    }
}

Then what I'd like to do is pass the user meta content to a hidden field like so:

function nf_hidden_field_values( $value, $field_type, $field_settings ) {
    global $post;
    $value = '';
    $user_id = get_current_user_id();
    
    if ( $field_settings['key'] == 'hidden_1603696346151' ) {
    
        $client_name = get_user_meta( $user_id, 'child_name', true )

        $num = wp_rand( 1, 100 );
        $value = $client_name.$num;
    }

    return $value;
}
add_filter( 'ninja_forms_render_default_value', 'nf_hidden_field_values', 9999, 3 );

The hidden field is getting sent to Zapier as a client id to sync to another application. The issue is the ninja_forms_render_default_value filter appears to execute before the user meta is updated in the first part of the code. If the user meta is empty, the 'child_name' meta is empty. If it has a value and I want to update it it is the old value. So I'm always one step behind.

So ideally that filter would be executed last, but I'm not sure if this is possible. I tried a wrapper function, but this didn't work, and I'm pretty sure that's not a sound approach.

I could just skip trying to use the 'child_name' meta and hard-code something, but it wouldn't be ideal.

Any help greatly appreciated.

Chris


Solution

  • Have a look at the 'ninja_forms_submit_data'-filter, you can there alter the field-data and also the user_meta_data.

    https://developer.ninjaforms.com/codex/submission-processing-hooks/