Generate Unique ID when Entry is Duplicated using GFAPI.

When an entry is duplicated using GFAPI, the Unique ID value is copied to the duplicated entry. This snippet generates a new unique ID value for the duplicated entry.

Code

Filename: gpuid-generate-new-uid-when-duplicated-with-gfapi.php

<?php
/**
 * Gravity Perks // Unique ID // Generate Unique ID when Entry is Duplicated using GFAPI.
 * https://gravitywiz.com/documentation/gp-unique-id/
 *
 * When an entry is duplicated using GFAPI, the Unique ID value is copied to the duplicated entry.
 * This snippet generates a new unique ID value for the duplicated entry.
 */
add_action( 'gform_post_add_entry', 'gw_regenerate_uid_for_duplicate_entry', 10, 2 );
function gw_regenerate_uid_for_duplicate_entry( $entry, $form ) {
	// Only process entry for a targeted form.
	if ( $form['id'] != 165 ) {
		return;
	}

	foreach ( $form['fields'] as $field ) {
		if ( is_a( $field, 'GF_Field_Unique_ID' ) ) {
			$uid = gp_unique_id()->get_unique( $form['id'], $field );
			GFAPI::update_entry_field( $entry['id'], $field->id, $uid );
			$entry[ $field->id ] = $uid;
		}
	}
}

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.