Description
Allows you to modify the generated Unique ID using a custom function.
This modifies a core aspect of this perk and may break your forms. Make sure that your custom function always returns a unique string by incorporating the
$unique
parameter in its output. Usage
add_filter( 'gpui_unique_id', 'my_custom_function', 10, 3);
Parameters
Examples
Prepend Unique IDs with a dynamic string
This example shows you how to prepend all generated IDs with the output of a custom function (my_custom_function
) before the value is stored in the database.
add_filter( 'gpui_unique_id', function( $unique, $form_id, $field_id ) {
// Prepend UIDs with a dynamically generated string
$my_pre = my_custom_function();
return $my_pre . $unique;
}, 10, 3 );
Reset Genereated ID when it reaches a number
This example shows you how to reset a sequentially generated ID when it reaches a specific number.
add_filter( 'gpui_unique_id', function( $unique, $form_id, $field_id ) {
// Update "123" to your form ID and "4" to the ID of your Unique ID field.
if( $form_id == 123 && $field_id == 4 ) {
// This is the number that the sequence will be reset to.
$starting_number = 1;
// When this number is reached, the unique ID sequence will reset to the starting number.
$reset_number = 2;
if ( $unique == $reset_number ) {
gp_unique_id()->set_sequential_starting_number( $form_id, $field_id, $starting_number - 1 );
}
}
return $unique;
}, 10, 3 );
Since
This filter is available since Gravity Forms Unique ID 1.3.13.