Ninja Forms Code Examples
Get a Form
In this example we get a form by its ID:
$form_id = 1;
Ninja_Forms()->form( $form_id );
Get Form Fields
In this example, we get all the fields from the form with ID 1:
$form_id = 1;
$form = Ninja_Forms()->form( $form_id );
$fields = $form->get_fields();
Get Field Keys
In this example, we get all the field keys for the form with ID 1:
$form_id = 1;
$form = Ninja_Forms()->form( $form_id );
$fields = $form->get_fields();
$fieldKeys = [];
foreach ( $fields as $field_id => $field ) {
$fieldKeys[ $field->get_setting( 'key' ) ] = $field->get_id();
}
ksort( $fieldKeys );
Get Submission by ID
In this example, we get the submission with ID 15 from the form with ID 1:
$form_id = 1;
$submission_id = 15;
$form = Ninja_Forms()->form( $form_id );
$submission = $form->get_sub( $submission_id );
If you want to get the submission without specifying the field ID, use this snippet:
$submission = Ninja_Forms()->form()->get_sub( $submission_id );
Get Submission by Field Value
In this example, we get all matching submissions, where a specified field with name code in the form has a value of 123:
$form_id = 1;
$code = '123';
$code_field_name = 'code';
$form = Ninja_Forms()->form( $form_id );
$fields = $form->get_fields();
$fieldKeys = [];
foreach ( $fields as $field_id => $field ) {
$fieldKeys[ $field->get_setting( 'key' ) ] = $field->get_id();
}
ksort( $fieldKeys );
$code_field_id = $fieldKeys[ $code_field_name ];
$where_condition = [ $code_field_id = $code ];
$matching_submissions = $form->get_sub( $where_condition );
Get Field Value by Field Name
In this example, we get a field value by specifying the fields name:
$form_id = 1;
$submission_id = 15;
$field_name = 'my_field_name';
$form = Ninja_Forms()->form( $form_id );
$fields = $form->get_fields();
$fieldKeys = [];
foreach ( $fields as $field_id => $field ) {
$fieldKeys[ $field->get_setting( 'key' ) ] = $field->get_id();
}
ksort( $fieldKeys );
$submission = get_sub( $submission_id );
$submission->get_field_value( $fieldKeys[ $field_name ] );