Support

Account

Home Forums ACF PRO Frontend User Registration Form Reply To: Frontend User Registration Form

  • This is a starting point specific to my own personal needs. I haven’t finished my form yet, but this would get you started. Would be good to branch out into validation once the fields are situated.

    You would need to update this with your own data since it has specific fields from $_POST currently. My fields were: first_name, last_name, user_email. The first and last name are already meta values so they work well for editing already, but I had to add support for editing the email address. Creating a new user, the point of this thread, was fully custom.

    Creates a form in your template based on a query variable.

    
    <?php
    if ( ! empty($_GET['user']) ) :
    	$user_id = ( is_numeric($_GET['user']) ? 'user_' . $_GET['user'] : 'new_user' );
    	acf_form([
    		'field_groups' => [ 510 ],
    		'post_id'      => $user_id,
    	]);
    endif;
    ?>
    

    In functions.php

    
    <?php
    /**
     * Add / Edit users
     */
    function trac_load_value_user_email( $value, $post_id, $field ) {
    	if ( false !== strpos($post_id, 'user_') && $user_id = str_replace('user_', '', $post_id) ) {
    		$userdata = get_userdata( $user_id );
    		if ( $userdata ) {
    			$value = $userdata->user_email;
    		}
    	}
        return $value;
    }
    add_filter( 'acf/load_value/name=user_email', 'trac_load_value_user_email', 10, 3 );
    
    function trac_update_userdata( $post_id ) {
    	if ( empty($_POST['acf']) || empty($_POST['acf']['field_599c48218c0ab']) || false === strpos($post_id, 'user') ) { return; }
    
    	$user_email = $_POST['acf']['field_599c48218c0ab'];
    
    	// Create a new user
    	if ( 'new_user' === $post_id ) {
    		// If email already exists, this should be part of validation or at least return an error...
    		if ( email_exists( $user_email ) ) { return false; }
    
    		// Create a password
    		$length = 13;
    		$include_standard_special_chars = false;
    		$random_password = wp_generate_password( $length, $include_standard_special_chars );
    		// Create the user, use email as username
    		$user_id = wp_create_user( $user_email, $random_password, $user_email );
    		// Update name and role
    		wp_update_user([
    			'ID' => $user_id,
    			'first_name' => ( ! empty($_POST['acf']['field_599c479c8c0a9']) ? $_POST['acf']['field_599c479c8c0a9'] : '' ),
    			'last_name' => ( ! empty($_POST['acf']['field_599c480e8c0aa']) ? $_POST['acf']['field_599c480e8c0aa'] : '' ),
    			'role' => 'editor',
    		]);
    	// Edit the user's email
    	} else {
    		$user_id = str_replace('user_', '', $post_id);
    		wp_update_user([
    			'ID' => $user_id,
    			'user_email' => $user_email,
    		]);
    	}
    }
    add_action( 'acf/save_post', 'trac_update_userdata', 1 );
    

    References:

    1. https://codex.wordpress.org/Function_Reference/wp_update_user
    2. https://www.advancedcustomfields.com/resources/create-a-front-end-form/