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);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.