Populate Values in REST API Request

Populates dynamically populated values if a value is not explicitly provided for fields that have values populated by Populate Anything.

Code

Filename: gppa-populate-values-in-rest-api-request.php

<?php
/**
 * Gravity Perks // Populate Anything // Populate Values in REST API Request
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Populates dynamically populated values if a value is not explicitly provided
 * for fields that have values populated by Populate Anything.
 */
add_action( 'rest_api_init', function () {
	if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
		return;
	}

	add_filter( 'gform_pre_submission_filter', function ( $form ) {
		$field_values = gp_populate_anything()->get_posted_field_values( $form );

		foreach ( $form['fields'] as &$field ) {
			$value = gp_populate_anything()->get_input_values( $field, 'value', $field_values );

			if ( $value && empty( $_POST[ 'input_' . $field->id ] ) ) {
				$_POST[ 'input_' . $field->id ] = $value;
			}
		}

		return $form;
	} );
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.