gform_form_post_get_meta

Description

This filter can be used to manipulate the Form Object; it is executed when the form meta is retrieved. Any changes made to the form object via this filter would affect form render, validation, submission, and the form in the admin.

When using this filter you wouldn’t need to make the same change using the gform_pre_render, gform_pre_validation, gform_pre_submission_filter, or gform_admin_pre_render filters.

Usage

The following would apply 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

Examples

1. Populate checkbox field

The following example dynamically populates a checkbox field with a list of published posts

//NOTE: update the '221' to the ID of your form
add_filter( 'gform_form_post_get_meta_221', 'populate_checkbox' );
function populate_checkbox( $form ) {

    foreach( $form['fields'] as &$field )  {

        //NOTE: replace 3 with your checkbox field id
        $field_id = 3;
        if ( $field->id != $field_id ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retreieved
        // more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
        $posts = get_posts( 'numberposts=-1&post_status=publish' );

        $input_id = 1;
        foreach( $posts as $post ) {

            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            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;
}

2. Return admin labels in REST API request

The following example shows how a form, in this case id #93, can be configured to return the admin labels when getting entries via the REST API with the _labels argument.

add_filter( 'gform_form_post_get_meta_93', function ( $form ) {
	if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST  || ! rgget( '_labels' ) ) {
		return $form;
	}

	foreach ( $form['fields'] as $field ) {
		if ( $field->displayOnly ) {
			continue;
		}
		$field->set_context_property( 'use_admin_label', true );
	}

	return $form;
} );

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

$form = gf_apply_filters( 'gform_form_post_get_meta', $form_id, $form );

This filter is located in GFFormDisplay::get_form_meta() in form_display.php.

Since

The form specific version of this filter was added in Gravity Forms 1.9.6.