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);
This solution (if ( empty( $value) worked fine for me for quite some time, but now I get a different unique ID again whenever I refresh the page. Has there been a change in WP that affects this?