Support

Account

Home Forums Add-ons Repeater Field Create Multiple Users from Repeater Field

Helping

Create Multiple Users from Repeater Field

  • I’m working with a Repeater Field where the Administrator will create a list of names and email addresses. When that list of names is saved (or updated), I would like to create new users based on the names and email addresses. Here is the code I have so far:

    add_filter('acf/update_value/name=team_member', 'add_new_user', 10, 3);
    function add_new_user( $value, $post_id, $field ) {
    	if(isset($value) && $value != '') {
    		
    		if( get_field('team_member') ) {
    			while( has_sub_field('team_member')) {
    		
    				$user_name = strtolower( get_sub_field('member_fname') ) . '_' . strtolower( get_sub_field('member_lname') ) ;
    				$pw = wp_generate_password( $length=12, $include_standard_special_chars=false );
    				$user_email = get_sub_field('member_email_address');
    
    		$value = wp_insert_user( array(
    					'user_login' => $user_name,
    					'user_pass' => $pw,
    					'user_email' => $user_email
    				));
    				
    			}		
    		}
    	}
    	return $value;
    }

    There are 2 problems with this code:
    1. It only creates a user account for the first name on the list
    2. For some odd reason, when the Options page is Saved/Updated, there are 30 new blank rows created on the options page.

    Any ideas on how to make this work? Thanks

  • I’m not certain, but two things I think you should try:

    1. Loop through your $value variable with a foreach instead of using the built in functions.
    2. You’re overwriting $value within the loop which is destructive. Return it untouched if you want the fields saved, or wipe it clean if you want to use the data but not save any of it. Either way, outside the loop.

    Hope those help.

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

The topic ‘Create Multiple Users from Repeater Field’ is closed to new replies.