Support

Account

Home Forums ACF PRO Create new user with acf_form

Solving

Create new user with acf_form

  • Hi!

    Is it possible to create a new user account with acf_form like described on this page:
    https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/

    I need the wordpress users to sync with mailchimp. I use different acf_fields on the user but they have no password to login to the page. So the form should be the standard fields like lastname/ firstname and some acf fields.

    Thanks for your help!

    • James

    • June 22, 2016 at 2:00 pm

    Hi @leuchterits

    I believe you can do it by using the acf/save_post hook. But, instead of creating a post, you need to create a user using the wp_create_user() function and delete the newly created post using the wp_delete_post() function. Maybe something like this:

    function my_acf_save_multiple_posts( $post_id ) {
        
        // get the user name field
        $first_name = get_field('first_name', $post_id);
        
        // get the user name field
        $last_name = get_field('last_name', $post_id);
        
        // create the user
        wp_create_user( $first_name . "_" . $last_name, 'random password here' );
        
        // delete the unused post
        wp_delete_post($post_id);
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_multiple_posts', 20);

    I hope this makes sense 🙂

  • Hey James!

    Thanks for your help, i tried to figure out how this works but im no step further. To add a new post i add the following code to my index.php. I dont know how this should look when i try to add a new user?

    Sorry and thanks for your help!

    <?php
    	
    	acf_form(array(
    		'post_id'		=> 'new_post',
    		'post_title'	=> true,
    		'post_content'	=> true,
    		'new_post'		=> array(
    			'post_type'		=> 'contact_form',
    			'post_status'	=> 'publish'
    		),
    		'return'		=> home_url('contact-form-thank-you'),
    		'submit_value'	=> 'Send'
    	));
    	
    	?>
    • James

    • June 26, 2016 at 12:16 am

    Hi @leuchterits ,

    Thanks a lot for getting back to me.

    You do not need to change anything on your ACF front-end form if its still saving as expected. What you need is to hook into the acf/save_post as described earlier.

    From here you can now call the native WordPress function wp_create_user() to create a user using the field values. https://codex.wordpress.org/Function_Reference/wp_create_user

    If you get stuck along the way, feel free to share with us a gist of your code and we will try to help 🙂

  • Hi James!

    Sorry but im no step further. When i use the code like posted above on my website:

    <?php
    	
    	acf_form(array(
    		'post_id'		=> 'new_post',
    		'post_title'	=> true,
    		'post_content'	=> true,
    		'new_post'		=> array(
    			'post_type'		=> 'contact_form',
    			'post_status'	=> 'publish'
    		),
    		'return'		=> home_url('contact-form-thank-you'),
    		'submit_value'	=> 'Send'
    	));
    	
    	?>

    I just get a form with a title and a wysiwyg-box. The form i need should have at least the fields user-firstname, user-lastname and some custom fields i added to the users.

    Sorry but i really have no idea how to create that. Should i give you access to my wp backend?

    Thanks Pascal

    • James

    • June 29, 2016 at 2:32 pm

    Hi @leuchterits

    You need to create a field group or add new custom fields to hold the first and last name. If you create a new field group, you can show it like this:

    acf_form(array(
        'field_groups' => array('99'),
    ));

    Where ’99’ is the ID of the field group. If you add new custom fields to the existing field group, then you don’t need to change anything.

    I hope this makes sense 🙂

  • Hey James…. sorry this gets embarrasing for me. But i still have no idea. I need to write a new user into my user database. The fields firstname and lastname exist without acf. I dont know how to create something like:

    <?php
    	
    	acf_form(array(
    		  'user_id'		=> 'new_user',
    	       'user_lastname'	=> 'formfield_lastname',
                'user_firstname'	=> 'formfield_firstname',
                'user_company_acf' => 'formfield_company'
    	
    ?>

    Im so sorry… but this function is essential for my website.

    • James

    • June 30, 2016 at 12:23 pm

    Hi @leuchterits

    I’ve attached the JSON export of the field group I used to create the new user. If you use the free version, please check the settings defined in the file. Here’s the acf_form() code I used to show the form:

    acf_form(array(
        'post_id' => 'new_post',
        'new_post' => array(
            'post_type' => 'post',
            'post_status' => 'draft'
        ),
    ));

    And this here’s the code I used in the functions.php file:

    function my_acf_save_user( $post_id ) {
        
        if( wp_is_post_revision( $post_id ) || is_admin() ) {
            return;
        }
        
        // get the user name field
        $first_name = get_field('first_name', $post_id);
        
        // get the user name field
        $last_name = get_field('last_name', $post_id);
        
        if ( $first_name && $last_name ) {
            // create the user
            wp_create_user( $first_name . "_" . $last_name, bin2hex(random_bytes(16)) );
            
            // delete the unused post
            wp_delete_post($post_id);
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_user', 20);

    If you think this is essential for your website but you’re unable to make it work, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.

    I hope this helps 🙂

    • mkeys

    • February 16, 2018 at 12:08 pm

    This is an old post, but I wanted to share what I think is a better method for anyone else who winds up here.

    I set the ‘post_id’ value of the acf_form function to ‘new_user’ which isn’t a registered value for this function, but we will get to that. It is worth noting that you can really write anything you want here as we override it later, but I think this communicates the intentions of the form well for anyone reading your code.

    acf_form( array(
    	'id'				=> 'register_new_speaker',
    	'post_id'			=> 'new_user',
    	'field_groups'			=> array(
    		'group_5a83681ebf931'
    	)
    ));

    Then I hook into ‘acf/pre_save_post’. I intercept the $_POST data being sent to the server, generate a new user using the submitted data, and then dynamically set the ‘post_id’ of the form to the new user ID.

    function generate_new_user_id( $post_id, $form ) {
    	// Check that we are targeting the right form. I do this by checking the acf_form ID.
    	if ( ! isset( $form['id'] ) || 'register_new_speaker' != $form['id'] ) {
    		return $post_id;
    	}
    
    	// Create an empty array to add user field data into
    	$user_fields = array();
    
    	// Check for the fields we need in our postdata, and add them to the $user_fields array if they exist
    	if ( isset( $_POST['acf']['field_5a83681ec4d01'] ) ) {
    		$user_fields['first_name'] = sanitize_text_field( $_POST['acf']['field_5a83681ec4d01'] );
    	}
    
    	if ( isset( $_POST['acf']['field_5a83681ec4d15'] ) ) {
    		$user_fields['last_name'] = sanitize_text_field( $_POST['acf']['field_5a83681ec4d15'] );
    	}
    
    	if ( isset( $_POST['acf']['field_5a85f62e356bc'] ) ) {
    		$user_fields['user_login'] = sanitize_user( $_POST['acf']['field_5a85f62e356bc'] );
    	}
    
    	if ( isset( $_POST['acf']['field_5a83689da2624'] ) ) {
    		$user_fields['user_email'] = sanitize_email( $_POST['acf']['field_5a83689da2624'] );
    	}
    
    	if ( isset( $_POST['acf']['field_5a83681ec4d36'] ) ) {
    		$user_fields['user_pass'] = $_POST['acf']['field_5a83681ec4d36'];
    	}
    
    	if ( isset( $_POST['acf']['field_5a83681ec4d01'], $_POST['acf']['field_5a83681ec4d15'] ) ) {
    		$user_fields['display_name'] = sanitize_text_field( $_POST['acf']['field_5a83681ec4d01'] . ' ' . $_POST['acf']['field_5a83681ec4d15'] );
    	}
    
    	$user_id = wp_insert_user( $user_fields );
    
    	if ( is_wp_error( $user_id ) ) {
    		// If adding this user failed, deliver an error message. I also do custom JS field validaiton before submit to check for proper email addresses, and to check for duplicate emails/existing usernames. But that code is beyond the scope of this thread
    		wp_die( $user_id->get_error_message() );
    	} else {
    		// Set the 'post_id' to the newly created user_id, including the 'user_' ACF uses to target a user
    		return 'user_' . $user_id;
    	}
    }
    add_action( 'acf/pre_save_post', 'generate_new_user_id', 10, 2 );

    From here out the ACF Form will submit like normal, but now it will save all of the custom fields to the newly created user. I like this method better than what is mentioned above, because the data gets saved directly to the ‘right spot’.

    Interesting thing to note: If you neglect to do the second part with the ‘acf/pre_save_post’ filter, ACF will save the data to wp_options (definitely not what you want to happen here).

    • dandc

    • April 10, 2018 at 4:35 pm

    this was really helpful, @mkeys. thanks for taking the time to add it.

  • This post has been so helpful and I really appreciate you posting this. I am wondering if someone could help just a bit. I am using the example from @mkeys and it is working. However in my use case I need the form to create the custom post type post as well as creating a new user. Can this function be altered to do both at the same time or do I need to create a generate post function for the same form and then add each field individually?

    Thank you for looking and please help!

    • i1100a

    • July 16, 2018 at 5:43 pm

    @cemfundog I have the same issue. Were you able to figure it out? Thanks

  • This is an old post – but I wondered if anyone could help: When I set this up, the first name and last name automatically were populated by the existing first and last name, but the same is not true for the email address. How can I link up the email address to the users email address?

    I should also mention, I suppose, that this doesn’t actually succeed in creating new users for me – but one step at a time.

  • Answering my own question: You must include acf_form_head() at the top of the page to process the form:
    https://www.advancedcustomfields.com/resources/acf_form/#notes

    • James

    • July 17, 2019 at 10:09 pm

    Hi @william.alexander

    Were you able to figure out the email saving issue as well?
    If not, kindly share your updated code so that we can advise further.

Viewing 15 posts - 1 through 15 (of 15 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.