Support

Account

Home Forums ACF PRO Frontend User Registration Form

Solving

Frontend User Registration Form

  • Hello

    I know about the acf form that can create posts from the frontend, is there a way to create a frontend form for user registration ?

    Any directions on how to do this please?

    Thank you

  • ACF can add fields to the user registration form, but as far as I know acf_form cannot be used to replace the user registration form, or at least I have not seen it done.

    But it should be possible using the idea here https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/ and using a custom acf/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/

    The main differences

    1) You would use something like ‘new_user’ as the post id in acf_form() and test for this value in the acf/pre_save_post filter

    2) Instead of creating a new post, you would create a new user, however that is done in WP… I don’t know, I’ve never done it.

    3) The post ID that you return from the acf/pre_save_post filter should be "user_{$user->ID}". This will tell acf to save the field values to the user instead of a post. https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

  • Any update on this? Looking to do the same.

  • 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/
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Frontend User Registration Form’ is closed to new replies.