Display Ninja Forms Data in Wordpress Template

1k Views Asked by At

I am having a complete nightmare trying to add an output of a ninja forms submission to a php template in wordpress. I want to display the contents of a given form submission by a user (the form requires the user to be logged in), used for calculating a quote on a template page displayed to a logged in user, but cannot seem to find the correct code to add it.

I have pasted the code I have so far below, and this displays using the submission plugin, however I cannot seem to select the submission based on the user that submitted it, and also cannot display in any sort of decent format.

This has taken many hours to get to this point and a little help would really be appreciated. I have used some of the code within the plugin and just require the user that submitted the form and some logical way to display it.

$form = Ninja_Forms()->form( 2 )->get();
    $fields = Ninja_Forms()->form( 2 )->get_fields();
    $subs = Ninja_Forms()->form( 2 )->get_subs();

                foreach( array_reverse( $subs ) as $sub ){
        $merge_tags = Ninja_Forms()->merge_tags[ 'fields' ];
        foreach( $fields as $field ){
            $field_id = $field->get_id();
            $merge_tags->add_field( array(
                'id' => $field->get_id(),
                'key' => $field->get_setting( 'key' ),
                'type' => $field->get_type(),
                'value' => $sub->get_field_value( $field_id )
            ));
        }
        echo $merge_tags->replace( $form->get_setting( 'subs_display' ) );
    }

Thanks in advance.

Matt

1

There are 1 best solutions below

0
On

You can add a condition based on the currently logged-in user's email e.g.

foreach( array_reverse( $subs ) as $sub ){
    if( wp_get_current_user()->user_email == $sub->get_field_value( 'email' ) ){
        $merge_tags = Ninja_Forms()->merge_tags[ 'fields' ];
        foreach( $fields as $field ){
            $field_id = $field->get_id();
            $merge_tags->add_field( array(
                'id' => $field->get_id(),
                'key' => $field->get_setting( 'key' ),
                'type' => $field->get_type(),
                'value' => $sub->get_field_value( $field_id )
            ));
        }
        echo $merge_tags->replace( $form->get_setting( 'subs_display' ) );
    }
}