Support

Account

Home Forums ACF PRO Create new user with acf_form Reply To: Create new user with acf_form

  • 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 🙂