Support

Account

Home Forums Backend Issues (wp-admin) Adding ACF form to user registration

Solving

Adding ACF form to user registration

  • I’m adding an ACF form to the user registration dialogue (/wp-login.php?action=register )

    Adding the form itself was easy using the register_form action:

    add_action('register_form', 'custom_add_registration_fields');
    
    function custom_add_registration_fields()
    {
    	if(function_exists('acf_form'))
    	{
    		if ( is_user_logged_in() === true )
    		{
    			global $current_user;
    			$user_id = 'user_'.$current_user->ID;
    		}
    		else
    			$user_id = 'user_new';
    
    		$args = array(
    			'post_id' => $user_id,
    			'field_groups' => array(142), //my field group
    			'form' => false
    		);
    
    		acf_form( $args );
    	}
    }

    Then I have hooked onto pre_save_post

    add_filter('acf/pre_save_post' , 'custom_pre_save_user' );

    Here is where my problem begins. The pre_save_post hook runs before WordPress own user_register hook. When pre_save_post runs I still have don’t know what ID the new user has (if the user form even validated!)

    So I would need to somehow defer ACFs form processing. Even disabling it altogether would be a solution (I can pick the values up from the $_POST array later when the user_register hook runs.)

    I would be happy to release the finished code for this, as I haven’t seen a complete solution for this problem. Thanks!

  • Hi @snitchhomer

    It seems you won’t be able to use the pre_save_post filter as it runs too early.

    Instead, perhaps you can use a more native WP action that is fired when registering a new user / saving a user.

    You can then take a look at the acf_form_head function source code to see how you would then save the ACF data.

    Thanks
    E

  • Thanks Elliot.

    How do I stop ACF from saving the form data?

    For example, when I passed $post_id => ‘new’ to acf_form(), an entry was created in the wp_options table with the key new…

    So can you tell me how to stop ACF from processing the form data? I think ACF is picking up the “acf_nonce” POST value, which triggers the form processing.

  • Hi @snitchhomer

    Are you using the acf_form_head function? If so, just remove this from the code and ACF will not run the save.

    If this is not the issue, perhaps ACF is listening to the saving of a new user and running it’s code there.

    This may be a limitation with ACF at the moment which will be fixed in ACF5 (very soon to be released).

    Maybe for now, you will have to add custom fields to the form in a manual way instead of acf_form

    Thanks
    E

  • Even when I remove acf_form_head, the ACF processing is triggered.

    I would very much appreciate if this can be fixed in ACF 5.

    Thanks.

  • Hi @snitchhomer

    ACF5 will be out in beta this week. Can I ask you to test it out with the above problem?

    Please note there are new location rules to add fields to a user registration form! So no custom code is needed.

    Thanks
    E

  • Hi Elliot,

    We have used another workaround (Showing a widget with fields the user can edit after they have registered.) We’re gonna stay on ACF 4 for a while, but I hope someone else can try this out.

  • If you want register user use this plugins:

    1. Display ACF on frontend use this: https://wordpress.org/plugins/acf-frontend-display/

    2. To process your form (example: create post, send email or register user) use this: https://wordpress.org/plugins/forms-actions/

  • Hi,

    this is now integrated with ACF 4.4.5, right? no extra plugins needed?

    thx

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

The topic ‘Adding ACF form to user registration’ is closed to new replies.