skip to Main Content

I have a custom post type named Student on WordPress that Adds new students. You can fill out the Students name, Student description and Student Admission Number.

I have also created a gravity form where a potential sponsor can submit their details when they want to sponsor a student. However, the sponsors need to fill in the Students Name and Student Admission Number on the form. Is it possible to have the fields Student Name and Student Admission Number populate dynamically on the gravity form?

The form is being displayed on the same page as the student post. The student name is the post title. The student registration number is a custom field created using advanced custom field plugin.The field label for student admission number is Student Admission Number and field name is student_admission_number on the custom field. On gravity form, we have Student Name and Student Admission Number as form fields.

How can I have the fields Student Name and Student Admission Number populate dynamically on the gravity form?

I tried adding the following code to my functions.php file but it did not work.

 // Add a custom shortcode to display the form
function custom_gravity_form_shortcode( $atts ) {
  $current_post = get_post();

  // Get the student name and admission number from the current post
  $student_name = $current_post->post_title;
  $student_admission_number = get_field('student_admission_number', $current_post->ID);

  // Get the form ID from the shortcode attributes
  $form_id = $atts['id'];

  // Prepare the form arguments
  $form_args = array(
    'field_values' => array(
      'student_name' => $student_name,
      'student_admission_number' => $student_admission_number,
    ),
  );

  // Render the form
  return gravity_form( $form_id, false, false, false, $form_args, true );
}
add_shortcode( 'gravityform', 'custom_gravity_form_shortcode' );

2

Answers


  1. Try the following (The shortcode you would use on the page is [stud_gravityform]:

    function custom_gravity_form_shortcode($atts = array()){
        $details = shortcode_atts( array(
            'form_id'=>'176'//change 176 to your form id
        ), $atts ); 
      $current_post = get_post();
    
      // Get the student name and admission number from the current post
      $student_name = $current_post->post_title;
      $student_admission_number = get_field('student_admission_number', $current_post->ID);
    
      // Get the form ID from the shortcode attributes
      $form_id = $details['form_id'];
    
      // Prepare the form arguments
      $form_args = array(
        'field_values' => array(
          'student_name' => $student_name,
          'student_admission_number' => $student_admission_number
        )
      );
    
      // Render the form
      return gravity_form( $form_id, false, false, false, $form_args, true );
    }
    add_shortcode( 'stud_gravityform', 'custom_gravity_form_shortcode' );//the gravityform shortcode is already taken so I changed it to "stud_gravityform"
    
    Login or Signup to reply.
  2. It might be easier to rely on the {custom_field} merge tag set in the default value setting of each field that should be dynamically populated. With this, it would fetch whatever custom field you specify from the post on which the form is embedded.

    enter image description here

    Less code. And easier to configure.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search