Get Session Hash

Use this function to get the parent hash that will be assigned to child entries created on in this session for a given form ID.

GPNF sets the hash via an AJAX call the first time the form is rendered. In order to reliably fetch the hash you must call this function before the gpnf_session_script_data filter is called.

Code

Filename: gpnf-get-session-hash.php

<?php
/**
 * Gravity Perks // Nested Forms // Get Session Hash
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Use this function to get the parent hash that will be assigned to child entries created on in this session for a
 * given form ID.
 *
 * GPNF sets the hash via an AJAX call the first time the form is rendered. In order to reliably fetch the hash you must
 * call this function before the `gpnf_session_script_data` filter is called.
 */
function gpnf_session_hash( $form_id ) {
	$session = new GPNF_Session( $form_id );
	return $session->get_runtime_hashcode();
}

/**
 * Here is an example of an appropriate time to fetch the parent hash.
 */
add_filter( 'gform_pre_render', function( $form ) {

	if ( wp_doing_ajax() ) {
		return $form;
	}

	$nested_form_fields = GFAPI::get_fields_by_type( $form, 'form' );
	if ( empty( $nested_form_fields ) ) {
		return false;
	}

	$hash = gpnf_session_hash( $form );

	// Add your code here!

	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.