Support

Account

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

Solving

Creating a unique ID in text field only once

  • The code below created a unique id to populate a text field called ‘measurement_id’. This works as expected.

    function yl_create_unique_id_acf_field( $value, $post_id, $field ) {
    	
    		$uniqueid_length	= 8; 
    		$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 = 'GM-' . $uniqueid;
    
    		return $title;
    
    }
    add_filter('acf/load_value/name=measurement_id', 'yl_create_unique_id_acf_field', 10, 3);

    The problem:
    After refreshing/updating the field/post, the unique id changes every time but I only want to add the unique id once.

    So only the first time, when the field is still empty and the post hasn’t been created yet, I want to populate the text field with the unique id. After saving/updating the post the unique id needs te stay the same forever.

    So I’m looking for I guess an IF EMPTY statement or something….

  • You can wrap the entire inner function in an if statement to avoid running any code if the field value is not empty.

    
    if ( empty( $value ) ) :
        // contents of {yl_create_unique_id_acf_field} function.
    endif;
    
  • 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?

  • Experience top-notch performance with our Cloud Server in the USA. Get reliable, scalable, and secure cloud solutions tailored to your business needs. Enhance productivity, reduce costs, and enjoy seamless operations with 24/7 support. Optimize your cloud journey with Lease Packet today!

  • 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);
    
  • Dive into the world of Satta Matka with easy-to-understand guides, daily tips, and expert insights. Discover the thrill of guessing, learn strategies, and boost your chances of winning. Join a community where fun meets fortune—start your journey today!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.