PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Updating field labels

  1. psm9640
    Member

    Hey guys - This might be a strange question but I'm stumped...

    What I'm trying to accomplish is have a single form that has a field label/question that is dynamically updated depending upon what page the user is on (questions are saved as a post meta value, the answer options are always "Yes" and "No").

    I've created a "dummy" form field in the form admin, but in order to grab the question that should replace the dummy field label, I need to pass the actual post ID of the current page to the filter and I can't figure out how to do it. I can make the ID available via a $_GET or $_REQUEST, and I don't need to update the answers since I know they'll always be yes and no.

    I've had success using gform_pre_render on updating actual values of form fields but I've hit a wall on this one. Thanks in advance for any insight/help.

    Posted 13 years ago on Tuesday December 28, 2010 | Permalink
  2. psm9640
    Member

    *UPDATE*

    Okay so I've now successfully targeted the label of the form field using this:

    add_filter('gform_pre_render_1', 'update_label');
    function update_label($form) {
    
        $question = 'This is a test question';
    
        foreach($form["fields"] as &$field)
            if($field["id"] == 24){
                $field["label"] = $question;
            }
    
        return $form;
    }

    So I guess all I need to figure out now is how to pass the post ID to the filter...will keep working on it.

    Posted 13 years ago on Tuesday December 28, 2010 | Permalink
  3. Give this a go:

    http://pastebin.com/fb0TR0Cv

    Quick Summary

    Using the gform_pre_render hook we can not only update the field value but also the field label. A few lines to note:

    global $post;

    This makes the post global object available to our hook function.

    $field_index = get_field_index_by_id($form['fields'], 1);

    This line sets the $field_index variable to the index of the field of whatever field ID you pass to the get_field_index_by_id() function. This function is just a little utility function to protect your code from any changes in field order you may make to the form.

    In this example "1" is the ID of the field we want to change the label for.

    $form['fields'][$field_index]['label'] = get_post_meta($post->ID, 'question', TRUE);

    This line sets the label of our selected field to the post meta value for the "question" post meta key for the current post. Update "question" to the name of the meta key you are using to store the question for each post.

    Hope that helps! :)

    Posted 13 years ago on Tuesday December 28, 2010 | Permalink
  4. psm9640
    Member

    This worked perfectly. Thank you David!

    Posted 13 years ago on Wednesday December 29, 2010 | Permalink