Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Perks // GP Unique ID // HyperDB Support for Sequential IDs
<?php
/**
* Gravity Perks // GP Unique ID // HyperDB Support for Sequential IDs
* http://gravitywiz.com/documentation/gravity-forms-unique-id/
*/
add_filter( 'gpui_sequential_unique_id_pre_insert', function( $uid, $form_id, $field_id, $starting_number) {
global $wpdb;
$wpdb->query( 'START TRANSACTION' );
$sql = $wpdb->prepare(
'INSERT INTO ' . $wpdb->prefix . 'gpui_sequence ( form_id, field_id, current ) VALUES ( %d, %d, ( @next := 1 ) ) ON DUPLICATE KEY UPDATE current = ( @next := current + 1 )',
$form_id, $field_id
);
$wpdb->query( $sql );
$uid = $wpdb->get_var( $wpdb->prepare( 'SELECT `current` from ' . $wpdb->prefix . 'gpui_sequence where form_id = %d and field_id = %d', $form_id, $field_id ) );
$wpdb->query( 'COMMIT' );
return $uid;
}, 10, 5 );
@Bassaddicted

This comment has been minimized.

Copy link

commented Dec 18, 2017

This works Thanks !

But I think line 17 should be updated to

$uid = $wpdb->get_var( $wpdb->prepare( 'SELECT `current` from ' . $wpdb->prefix . 'gpui_sequence where form_id = %d and field_id = %d', $form_id, $field_id ) );

to work in multisite as we aren't statically calling wp_gpui_sequence in the query.

@spivurno

This comment has been minimized.

Copy link
Owner Author

commented Dec 19, 2017

Thanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.