Using Gravity Forms ‘gform_form_post_get_meta’ PHP action

The gform_form_post_get_meta Gravity Forms PHP filter allows you to manipulate the Form Object when the form meta is retrieved. Any changes made to the form object through this filter will affect form render, validation, submission, and the form in the admin. You don’t need to use gform_pre_render, gform_pre_validation, gform_pre_submission_filter, or gform_admin_pre_render filters when using this filter.

Usage

To apply the filter to all forms:

add_filter('gform_form_post_get_meta', 'your_function_name');

To limit the scope of your function to a specific form, append the form id to the end of the hook name (format: gform_form_post_get_meta_FORMID):

add_filter('gform_form_post_get_meta_6', 'your_function_name');

Parameters

More information

See Gravity Forms Docs: gform_form_post_get_meta

Examples

Populate checkbox field

Populate a checkbox field with a list of published posts:

add_filter('gform_form_post_get_meta_221', 'populate_checkbox');

function populate_checkbox($form) {
    foreach ($form['fields'] as &$field) {
        $field_id = 3;
        if ($field->id != $field_id) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retrieved
        $posts = get_posts('numberposts=-1&post_status=publish');

        $input_id = 1;
        foreach ($posts as $post) {
            if ($input_id % 10 == 0) {
                $input_id++;
            }

            $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
            $inputs[] = array('label' => $post->post_title, 'id' => "{$field_id}.{$input_id}");
            $input_id++;
        }

        $field->choices = $choices;
        $field->inputs = $inputs;
    }

    return $form;
}

Return admin labels in REST API request

Configure a form, in this case id #93, to return the admin labels when getting entries via the REST API with the _labels argument:

add_filter('gform_form_post_get_meta_93', 'add_admin_label');

function add_admin_label($form) {
    foreach ($form['fields'] as &$field) {
        if (!empty($field->adminLabel)) {
            $field->label = $field->adminLabel;
        }
    }

    return $form;
}

Set default value for a specific field

Set a default value for a field with ID 5:

add_filter('gform_form_post_get_meta_15', 'set_default_value');

function set_default_value($form) {
    foreach ($form['fields'] as &$field) {
        if ($field->id == 5) {
            $field->defaultValue = 'Your default value';
        }
    }

    return $form;
}