Generate Unique ID when Entry is Duplicated in GravityView

Generates a new unique ID value for entries duplicated in GravityView.

Code

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

<?php
/**
 * Gravity Perks // Unique ID // Generate Unique ID when Entry is Duplicated in GravityView
 * https://gravitywiz.com/documentation/gp-unique-id/
 *
 * When an entry is duplcicated in GravityView, the Unique ID value is copied to the duplicated entry.
 * This snippet generates a new unique ID value for the duplicated entry.
 *
 * Plugin Name: GP Unique ID – GravityView Duplication Support
 * Plugin URI:  https://gravitywiz.com/documentation/gp-unique-id/
 * Description: Generates a new unique ID value for entries duplicated in GravityView.
 * Author:      Gravity Wiz
 * Version:     0.1
 * Author URI:  https://gravitywiz.com
 */
add_action( 'gravityview/duplicate-entry/duplicated', function ( $duplicated_entry = array(), $entry = array() ) {
	if ( ! is_callable( 'gp_unique_id' ) ) {
		return $duplicated_entry;
	}

	$form = GFAPI::get_form( $duplicated_entry['form_id'] );
	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( $duplicated_entry['id'], $field->id, $uid );
			$duplicated_entry[ $field->id ] = $uid;
		}
	}
	return $duplicated_entry;
}, 10, 2 );

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.