Skip Navigation

[Closed] Create form that lists users and assigns specific role to chosen user

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 3 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#2003355

Tell us what you are trying to do?

I am trying to create a form that lists all of the users on the site and then when the user is selected and form submitted, it assigns that user to a specific role.

#2003687
popup.png

Hello, Toolset's Edit User Forms are designed to edit one specific User, not to allow the User to select a User to edit. There is also no built-in way to change User roles in a Form, so to get around those limitations, you would need some custom code and a special generic field with options defined by a View of Users. If you're okay with that, I can give you some guidance for this setup.

Create an Edit User Form that edits the current logged-in User. Use expert mode to expose all the Form shortcodes. Delete all the visible inputs from the Form except the submit button, and leave the Form Messages shortcode in place. Note that if you leave any inputs in the Form, you will edit the current logged-in User's profile in addition to changes the selected User(s)'s role.

In that mostly empty Form, create a generic input field that includes options for each User in the database. You could use any generic field that offers multiple options - select, radio, checkboxes, or multiple select fields would be ideal. Select fields and radio fields only allow one User selection per submission. Checkboxes and multiple select fields would allow the User to select more than one User and edit those Users' roles in a single submission.

The generic field popup will allow you to set the data source to be a shortcode, so you can place a Views shortcode here to display options for each User (popup.png). To supply those options, create a View of Users that outputs the results in JSON-like format. The idea is to create data output formatted like this:

{"value": "123", "label": "johndoe123"}, {"value": "456", "label": "janesmith456"}

That will give the generic field the data it needs to generate dynamic options for each User. When creating the View in the legacy editor, use the Loop Wizard to generate the list format "List with separators" and insert the User ID in the loop builder popup. This step is important because the system will generate the appropriate loop scaffolding for you, automatically strip out spaces and line breaks from the output, and automatically disable the wrapping div around the View. After using the loop wizard, you should then replace the contents with the following:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
			[wpv-item index=other]
				{"value": "[wpv-user field='ID']", "label": "[wpv-user field='user_login']"},
			[wpv-item index=last]
				{"value": "[wpv-user field='ID']", "label": "[wpv-user field='user_login']"}
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

You can modify the label by adjusting the contents in both wpv-item blocks. The text johndoe123 and janesmith123 represent the user logins, which will be shown as the labels for each option on the front-end, but you could use any arbitrary text label here. I suggest login because it's a required field and it must be unique for each User. You are free to add or replace that with any other User info. Views wpv-user shortcode documentation: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-user
Or if you want to display information from a User custom field: https://toolset.com/documentation/customizing-sites-using-php/functions/

Place the User View shortcode in the generic field builder to display an input field with options for all Users. The builder will generate the appropriate shortcode for you, something like this:

[cred_generic_field type='multiselect' field='generic-users-multi']
{
"required":0,
"options":[ [wpv-view name="generic-multiselect-user-options"] ]
}
[/cred_generic_field]

Let's pause here. I'd like to get your feedback and see if you are able to get this implemented so far before moving along to the next steps.

The topic ‘[Closed] Create form that lists users and assigns specific role to chosen user’ is closed to new replies.