Gravity Forms: Dynamic creation of fields from CPT
How can I dynamically create set of fields in my Gravity Form, based on my custom post type?
We created a mu-plugin called gravity.php for that:
<?php /* * Add fields to Gravity Form */ // add fields from CPT 'brochures' to Gravity Form #3 add_filter( 'gform_pre_render_3', 'wpster_add_fields_to_gravity' ); add_filter( 'gform_pre_validation_3', 'wpster_add_fields_to_gravity' ); add_filter( 'gform_pre_submission_3', 'wpster_add_fields_to_gravity' ); add_filter( 'gform_pre_submission_filter_3', 'wpster_add_fields_to_gravity' ); add_filter( 'gform_pre_process_3', 'wpster_add_fields_to_gravity' ); function wpster_add_fields_to_gravity( $form ){ // get all CPT 'brochures' $query = new WP_Query(array( 'post_type' => 'brochures', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', )); $i = 0; while ($query->have_posts()) { $query->the_post(); $brochures[$i]['post_title'] = get_the_title(); $i++; } // add CPT to form $field_product_id = 200; $calculated_formula = ""; foreach($brochures as $brochure): $field_label = $brochure['post_title']; $props = array( 'id' => $field_product_id, 'label' => $field_label, 'type' => 'product', 'inputType' => 'singleproduct', 'isRequired' => false, 'basePrice' => 0, 'enableCalculation' => true, 'allowsPrepopulate' => true, 'cssClass' => 'magicForm', 'inputs' => [ 0 => [ 'id' => $field_product_id . '.1', 'label' => 'Product Name', 'name' => 'param_product' ], 1 => [ 'id' => $field_product_id . '.2', 'label' => 'Price', 'name' => 'param_price' ], 2 => [ 'id' => $field_product_id . '.3', 'label' => 'Quantity', 'name' => 'param_qty' ] ] ); if (!in_array($field_product_id,array_column($form['fields'], 'id'))) { $field = GF_Fields::create( $props ); array_push( $form['fields'], $field ); $calculated_formula.= "+{:".$field_product_id.".3}"; } $field_product_id++; endforeach; // calculate quantity $field_quantity_id = 100; $props = array( 'id' => $field_quantity_id, 'label' => 'Quantity ordered', 'type' => 'number', 'enableCalculation' => true, 'allowsPrepopulate' => true, 'calculationFormula' => $calculated_formula, ); if (!in_array($field_quantity_id,array_column($form['fields'], 'id'))) { $field = GF_Fields::create( $props ); array_push( $form['fields'], $field ); } // calculate shipping cost / formula need Gravity Forms Advanced Calculations $field_id = 101; $calculated_formula = "if( {:".$field_quantity_id."} < 10 ):\n0\n elseif( {:".$field_quantity_id."} <= 50 ):\n20\n else:\n50\n endif;"; $props = array( 'id' => $field_id, 'label' => 'Shipping Cost', 'type' => 'number', 'enableCalculation' => true, 'allowsPrepopulate' => true, 'calculationFormula' => $calculated_formula, ); if (!in_array($field_id,array_column($form['fields'], 'id'))) { $field = GF_Fields::create( $props ); array_push( $form['fields'], $field ); } // html field $field_id = 102; $props = array( 'id' => $field_id, 'label' => 'HTML', 'type' => 'html', 'content' => '<h3>Frais de port:</h3><10 : CHF0, <=50 : CHF20, au-delà : CHF 50', ); if (!in_array($field_id,array_column($form['fields'], 'id'))) { $field = GF_Fields::create( $props ); array_push( $form['fields'], $field ); } if ( GFForms::get_page() !== 'form_editor' ) { return $form; } } ?>