Support

Account

Home Forums ACF PRO Generate random code

Solving

Generate random code

  • Hello everyone.
    I will want to add a field that would generate me a unique random code and that will be read-only. it is to make referral code. ex: alf-25698735.

    A big thank you for your help

  • I’ve done similar using the below code:

    <?php
    add_action( 'transition_post_status', 'generate_random_url', 10, 3 );
    
    function generate_random_url( $new_status, $old_status, $post ) {
    
        #only apply this to a particular post type 	
    	if ( 'publish' !== $new_status or 'publish' === $old_status || 'cpt' !== get_post_type( $post ) )
    	return;
    	
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    	return;	
            
            ####generate the random slug
            
            #get current date/time
            $date = date('YmdH:i:s');
            
            #randmoise
            $randomise = ($date * 25);
            
            #hash the randomised value, just adds another level but doesn't need to be 100% secure
            $new_slug = md5($randomise);
                    
            #get the post data
            $my_post = array(
                'ID'        => $post->ID,
                'post_name'    => $new_slug
            ); 
        
            #update the post into the database
            wp_update_post( $my_post );
    		
    		#update the download ID field
    		update_post_meta( $post->ID, 'download_id', $new_slug );
    
    }

    Basically, once the custom post type was saved, it would generate a random value based on a date time stamp.
    I used this to create a random page URL but you can see it also updated a custom field called download ID

    I’m sure there are other ways to do this but should get you underway.

  • I am really a beginner I do not know how to achieve what I want from your code. I want a simple trick just to generate a random number when registering a user

  • I suggest you look at do_action( ‘user_register’, int $user_id, array $userdata )

    Which fires immediately after a new user is registered.

    So as an example (code untested), you could look at something like:

    <?php
    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
     
    function myplugin_registration_save( $user_id ) {
     
    	####generate the random number
    
    	#get current date/time
    	$date = date('YmdH:i:s');
    
    	#randmoise
    	$randomise = ($date * 25);	
    
    	update_user_meta($user_id, 'your_acf_field_name', $randomise );
     
    }

    You need to setup the ACF field and link it to the user role(s). You then need to update the above example with the field name.
    Add the code to your function file and test it.
    If it doesn’t work, look to try and debug why.

  • Well, I’m going to give up it’s too complicated I don’t understand that this function does not exist natively. lots of form plugins have it

  • Did you even try adding the code to your functions file? It should be pretty much good to go subject to swapping the field name and testing.

    Plugins whilst help, try to avoid adding them if you can.

    Better to add code you know than constant 3rd party plugins which may become vulnerable (in my honest opinion)

  • I tried but it doesn’t work

  • Just tested and works fine

    Here’s what I did:
    1) Create an ACF text field called Random String
    2) This creates the label random_string
    3) For testing, I assigned this field to User Role is equal to All
    4) I added the below code to my functions.php file:

    <?php 
    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
    function myplugin_registration_save( $user_id ) {
     
    	####generate the random number
    
    	#get current date/time
    	$date = date('YmdH:i:s');
    
    	#randmoise
    	$randomise = ($date * 25);	
    
    	update_user_meta($user_id, 'random_string', $randomise ); #change random_string to your ACF field label!!!
     
    }

    5) I registered a new user
    6) I then clicked to edit the user, scrolled down to my new Random String field, this was the result: 50527810375

    Code and entire process tried and tested!

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.