Support

Account

Home Forums General Issues Creating a unique ID in text field only once Reply To: Creating a unique ID in text field only once

  • It’s been fixed, it only generates the unique ID once now.

    function yl_create_unique_id_acf_field( $value, $post_id, $field ) {
    
    	// Check if the field already has a value
    	if ( !empty( $value ) ) {
    		return $value; // If it already has a value, return it.
    	}
    
    	// If no value, generate a new unique ID
    	$uniqueid_length = 9;
    	$uniqueid = crypt(uniqid(rand(), 1));
    	$uniqueid = strip_tags(stripslashes($uniqueid));
    	$uniqueid = str_replace(".", "", $uniqueid);
    	$uniqueid = strrev(str_replace("/", "", $uniqueid));
    	$uniqueid = substr($uniqueid, 0, $uniqueid_length);
    	$uniqueid = strtoupper($uniqueid);
    
    	$title = 'WRE-' . $uniqueid;
    
    	// Save the generated ID to the ACF field
    	update_field( 'account_id', $title, $post_id );
    
    	return $title;
    }
    add_filter('acf/load_value/name=account_id', 'yl_create_unique_id_acf_field', 10, 3);