Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active September 2, 2020 02:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spivurno/b25723b0f17c1bd28ab6c5e9100fc506 to your computer and use it in GitHub Desktop.
Save spivurno/b25723b0f17c1bd28ab6c5e9100fc506 to your computer and use it in GitHub Desktop.
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 );
@gabrielcatellier
Copy link

gabrielcatellier 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
Copy link
Author

Thanks for the update!

@claygriffiths
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment