How to update WordPress user with Fluent Forms

This is a question I see quite commonly on the Fluent Forms Community Facebook page, so here's a quick tutorial on how to use the Fluent Forms 'fluentform_submission_inserted' action hook to update user data.

Essentially, you create a custom function which updates the user, and hook it into this action so that it triggers when the form is submitted.

Update a user ACF field with Fluent Forms

Create a new code snippet, and give it an appropriate title. Then copy-paste the code below into it and save. Check the notes to see how it works and how to adapt it, and activate when ready.

add_action('fluentform_submission_inserted', 'smoothw_ff_acf_user_update', 20, 3);
function smoothw_ff_acf_user_update($entryId, $formData, $form) {
	if($form->id == 3) { // should be the ID of the correct form, so that if not this form, function does not run

		$ff_data = $formData['example_ff_field']; // grab the field data from the form object by using the name attribute under Advanced in FF
		$user_id = get_current_user_id(); // we update the current user
		
		// update ACF field on user
		$acf_user = 'user_' . $user_id; // for use in the update_field function, see https://www.advancedcustomfields.com/resources/update_field/
		if($ff_data) { // if there is FF value
			update_field('example_acf_field', $ff_data, $user_id); // update the ACF field with FF value
		}

	}
}

However, you'll need to use a different method if you are working with standard WordPress meta fields, such as those created by JetEngine.

Update a user meta field with Fluent Forms

In this case, we will use the WordPress function update_user_meta.

add_action('fluentform_submission_inserted', 'smoothw_ff_user_meta_update', 20, 3);
function smoothw_ff_user_meta_update($entryId, $formData, $form) {
    if($form->id == 3) { // should be the ID of the correct form, so that if not this form, function does not run

        $ff_data = $formData['example_ff_field']; // grab the field data from the form object by using the name attribute under Advanced in FF
        $user_id = get_current_user_id(); // we update the current user

        // update meta field on user
        $field_to_update = 'example_field_name'; // Field Name/ID, for use in the update_user_meta function
        if($ff_data) { // if there is FF value
            update_user_meta( $user_id, $field_to_update, $ff_data ); // update the meta field with FF value
        }

    }
}

And here's how to change the default user fields such as first_name, last_name, user_email, and so on.

Update default WordPress user fields with Fluent Forms

What if it's not meta fields you want to update? Create a snippet and adapt this to suit:

add_action('fluentform_submission_inserted', 'smoothw_ff_user_update', 20, 3);
function smoothw_ff_user_update($entryId, $formData, $form) {
	if($form->id == 3) { // should be the ID of the correct form, so that if not this form, function does not run

		$ff_data = $formData['example_ff_field']; // grab the field data from the form object by using the name attribute under Advanced in FF
		$user_id = get_current_user_id(); // we update the current user

		// update user fields - credit to https://wp-kama.com/function/wp_update_user for the list
		// comment out the user fields you do not want to update
		// add $ff_data to the part you want to update, in this example we are updating the user_url (website)
		$userdata = array(
			'ID'              => $user_id, // the user to update
			'user_pass'       => '',
			'user_login'      => '',
			'user_nicename'   => '',
			'user_url'        => $ff_data,
			'user_email'      => '',
			'display_name'    => '',
			'nickname'        => '',
			'first_name'      => '',
			'last_name'       => '',
			'description'     => '',
			'rich_editing'    => true, // false - disable the visual editor
			'user_registered' => '',   // registration date (Y-m-d H:i:s)
			'role'            => '',   // (string) user role
		);
		
		// update the userdata according to the values defined above
		wp_update_user($userdata);

	}
}

Hope that helps! If you need this adapted to your personal use case, or you want help with something else, reach out to me at [email protected] for my rates.

crossmenu linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram