Update User Settings
-
Hi Konrad,
I have created a user in wordpress’ user backend and wanted to allow the user to update their user profile once logged in via your acf extended form.
I have tried all sorts of combinations and haven’t managed to get it to work. I can’t find any documentation on your site either for the action: Update User
This is what I’ve done: I have created an acf field group with the typical user detail fields i.e. username, password (to log in) and other fields that I want the user to update like first_name, last_name, description, Linkedin_Profile….
Can I use the Action: Update User to load the user field group upon successful user login and then allow the user to update the user profile details with the other user inputs on the ACFE form?
Is it also possible to load the existing details if they exist when the user logs in just using their username and password?
Many thanks for your great support!
-
I also noted that using the update user action also allows us to create a new user. Is there a code snippet or setting we can use just to enable updating an existing user and not creating a new one?
Many thanks again.Hello,
Thanks for the feedback!
It seems you’re becoming increasingly interested in the ACF Extended Front-End Form feature. As we’ll probably be discussing it quite often (here or in future tickets), and due to the complexity of this module, starting from now I’d like to ask you to provide a Json Export of your Form configuration each time you make some modification or ask about a logic you try to setup.
This will allow me to check your current form settings, provide customized answers, and avoid unnecessary back-and-forth to get more details. You can export your Form Json code directly from your From UI, in the top right corner “Export: Json”. See screenshot.
You can then share it here, using the
code block
, like this:[
{
"name": "my-form",
"title": "My Form",
"field_groups": [
"group_667de84d73d22"
],
...Regarding the Form Documentation, you probably missed it, but there are documentation links directly in the Form UI, including for the Actions you use. See screenshot.
Here is the main Form Documentation. You’ll find useful explanations about customization in the PHP Integration documentation here. Regarding the User Action, the documentation is here. Please take a look at it, as you’ll find very useful information, including PHP/JS hooks for advanced usages.
Regarding your question about the “User Action”, yes you can totally use it to display a “My Profile” type of form that allows the “Currently Logged User” to edit their information.
To do so, you have to set the “User Action” in “Update User” Mode. This mode use the
wp_update_user()
WP function behind the scene (see documentation), and there is no instance where using “Udpate User” would “Create a user” as far as I know. It simply update an existing user.You probably thought the “Save” tab is a “Creation”, but it is not. The “Save” tab allows you to define which field to “Save” (during a “creation” or “update”), and the “Load” tab to define which field should have their values “loaded”.
When you switch the mode to “Update User”, you have to select which user should be updated in the “Save > Target” setting. In your case, you can use “Target: Current User”, so the fields will update the currently logged user (See screenshot).
Then head over the “Load” tab, and enable the “Load Values” switch. Select which “Source” should be used to load field values from. As it is the same as the target, you can also use “Source: Current User” (See screenshot).
Finally, choose which ACF Field should be used to save & load the native WP User Object: Username, Email, Description etc… And if you have custom ACF Fields saved as metadata (such as “Linkedin Profile” which is not part of the native WP User object), make sure to select them in the “Save ACF Fields” in the Save tab, and the “Load ACF Fields” in the Load tab (following the logic of your previous ticket).
Small note regarding the Password field. This field will never have any value “loaded”. This is because WordPress doesn’t store users password in a readable format in the database, so nothing can be displayed here. I would suggest to follow to good practices of every major websites: make a specific form where the user just update their password.
As a conclusion, here is a video demo of a simple Update User form. If you want to test it, here is the ACFE Form Json export, and the corresponding ACF Field Group Json export.
Save each json code in a file, and import the Form from the “ACF > Tools > Import Forms” tool, and the Field Group from the “ACF > Tools > Import” Tool, in the WP Admin menu.
Hope it helps!
Have a nice day!
Regards.
Hi Konrad,
Apologies for the delay getting back to you, and thanks for your continued support. As part of testing my user form I created a new user in the wordpress user admin. I then tried to load the user in the form to change their first name etc. It failed. I loaded the form in an incognito browser page so that no existing users were logged in,
In the form I then inputed the correct username and password as set in the admin section, updated the other acf fields and submitted the form. The user details in the user admin were not updated, nor were the fields loaded when I input the correct username and password – I’m not usre this is supposed to happen when the submit button is not pressed.
Here is my json:[
{
"name": "update-user-test-form",
"title": "Update User Test Form",
"active": true,
"field_groups": [
"group_66e4ba409b412"
],
"settings": {
"location": false,
"honeypot": true,
"kses": true,
"uploader": "default"
},
"attributes": {
"form": {
"element": "form",
"class": "",
"id": ""
},
"fields": {
"element": "div",
"wrapper_class": "",
"class": "",
"label": "top",
"instruction": "label"
},
"submit": {
"value": "Submit",
"button": "<input type=\"submit\" class=\"acf-button button button-primary button-large\" value=\"%s\" \/>",
"spinner": "<span class=\"acf-spinner\"><\/span>"
}
},
"validation": {
"hide_error": false,
"hide_revalidation": false,
"hide_unload": false,
"errors_position": "above",
"errors_class": "",
"messages": {
"failure": "Validation failed",
"success": "Validation successful",
"error": "1 field requires attention",
"errors": "%d fields require attention"
}
},
"success": {
"hide_form": false,
"scroll": false,
"message": "Form updated",
"wrapper": "<div id=\"message\" class=\"updated\">%s<\/div>"
},
"actions": [
{
"action": "user",
"name": "",
"type": "update_user",
"validation": false,
"save": {
"target": "{user}",
"user_email": "{field:field_66e4d67466818}",
"user_login": "{field:field_66e4ba4166815}",
"user_pass": "{field:field_66e4d60766817}",
"first_name": "{field:field_66e4d6f766819}",
"last_name": "{field:field_66e4d72f6681a}",
"nickname": "",
"display_name": "{field:field_66e4e1e796f20}",
"user_url": "",
"description": "",
"role": "subscriber",
"log_user": false,
"acf_fields": [
"field_66e4d60766817",
"field_66e4d67466818",
"field_66e4e52b0c889",
"field_66e4d6f766819",
"field_66e4d72f6681a",
"field_66e4e1e796f20"
]
},
"load": {
"source": "{user}",
"user_email": "",
"user_login": "field_66e4ba4166815",
"user_pass": "",
"first_name": "field_66e4d6f766819",
"last_name": "field_66e4d72f6681a",
"nickname": "",
"display_name": "field_66e4e1e796f20",
"user_url": "",
"description": "",
"role": "",
"acf_fields": [
"field_66e4d60766817",
"field_66e4d67466818"
]
}
}
],
"render": ""
}
]I’m sorry, but I’m obviously missing something here, and I’m sorry to be a pain!
Hello,
Thanks for the details and export!
It looks like you’re misinterpreting what “Current User” means. Your form “User Action” (and the form demo I shared) will update the “currently logged user” who view the form.
Typically, if you’re logged as “Admin”, your form will allow you to update the “Admin” user. Most of websites call this page “My Profile”. Where the user, who’s logged-in, can change his name, email etc…
If you try to use this form with non-logged user (incognito), then it won’t update anything, as there is no “Current User”.
Regarding your second use case, where you try to input the username + password, and update other fields within the same form: this application is not possible without some advanced custom PHP logic. Typically what you try to achieve is a login + update values within the same form.
I wouldn’t recommend to do that, as the form cannot pre-fill the name, first, last name etc… (since the user is not logged-in yet). This use case is kind of weird to be honest and prone to errors, since the fields cannot be prefilled, and if the user forget to fill one field (example: website), it will be saved as empty, and erase the previous value as empty. This is probably why I cannot remember any website that use this scenario.
I would rather recommend to follow common websites applications practices:
– 1 Form: “Login”
Where the visitor can login with a simple login/password fields, on a page called “Login”. You can achieve that with a new form, and a “User Action” set to “Login”.
– 1 Form: “My Profile”
Where the user (once logged-in) can edit his details, on a page called “My Profile”. You can achieve that with the demo form I shared with you. See my previous answer.
Hope it helps!
Have a nice day!
Regards.
- You must be logged in to reply to this topic.