Close

July 4, 2017

Formidable Forms: Set Entry Key as UUID

The following code snippet sets the entry key as a UUID in Formidable Forms

/* Change Entry Key to UUIDv4 */
add_filter('frm_validate_entry', 'change_entry_key', 20, 2);
function change_entry_key($errors, $values){
  if(($_POST['frm_action'] == 'create')){
    $_POST['item_key'] = guidv4();
  }
return $errors;
}

/* 
	Generate a version 4 UUID
  	clarification: a v4 UUID is in the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
  	where any x is a random alphanumeric character
  	4 is the numeral 4
  	and y may only by one of 8, 9, A or B
*/
function guidv4()
{
    $data = openssl_random_pseudo_bytes(16);
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0010
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

Leave a Reply

Your email address will not be published. Required fields are marked *