Skip Navigation

[Résolu] Create dropdown with users

This support ticket is created Il y a 4 années et 11 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 29 réponses, a 3 voix.

Dernière mise à jour par Christian Cox Il y a 4 années et 11 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1377451

How can I create a dropdown/select field in a CRED form that displays all wordpress users?

Thanks!

#1377461

Hello, you would create a generic select field, then populate the options for this field with a View of Users. To populate the options with a View, create a View of Users. In the Loop Output editor, click Loop Wizard and choose "list with separators". Insert the User ID, then then system will generate some loop code for you. Delete the loop code and replace it with this loop code:

Create a View of Courses. In the Loop Output editor, click "Loop Wizard" and choose "List with separators". Insert the post title and the system will generate a loop for you. Replace the loop contents with this:
[php]
<wpv-loop>
  [wpv-item index=1]
  {"value":"[wpv-user field='ID']","label":"[wpv-user field='display_name']"}
  [wpv-item index=other]
  ,{"value":"[wpv-user field='ID']","label":"[wpv-user field='display_name']"}
</wpv-loop>

Now place this View in the generic field's options, like this:

[cred_generic_field field="your-generic-users-slug" type="select" class="" urlparam=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"",
"options":[[wpv-view name="your-users-list-view"]] 
}
[/cred_generic_field]

You'll probably need to use the CRED API to capture the selected User and do something with it. We have documentation for that API available here: https://toolset.com/documentation/programmer-reference/cred-api/

#1377465

It's sorta working.

It prints :

{"value":"2","label":"Qualty Admin"} ,{"value":"3","label":"test2 test2"} ,{"value":"4","label":"test3 test"}

to the form page.

Also, do I need to modify "your-generic-users-slug" ???

Thanks!

#1377469

You can modify that slug if you want, it's arbitrary. Perhaps it would be helpful for me to log in and see how you have this set up? Please provide login information in the private reply fields here.

#1377483

BTW - You can access FTP via the plugin installed in the dashboard.

#1377485
Screen Shot 2019-11-06 at 1.56.52 PM.png

It looks like the View is placed in the Form twice, so it's showing up once outside the select field. You should delete the View that is placed outside the generic select if you don't want it to display the User list visibly in the Form. I made a minor adjustment in the View and disabled the wrapping div (see the screenshot), and now the select field options are displayed in the dropdown. Please check.

#1377507

This is fantastic! Two final questions:

1 - Is there a way to set the default user in the dropdown?
2 - is it true that if I turn off expert mode this field will get deleted? Is there a way to add it to the field group?

Thanks!

#1377515

1 - Is there a way to set the default user in the dropdown?
Yes, in the generic select field you can set the "default" value to be the ID of whatever User you want to show up selected by default. In this example, that is User with ID 123:

[cred_generic_field field="your-generic-users-slug" type="select" class="" urlparam=""]
{
"required":1,
"validate_format":0,
"persist":1,
"default":"123",
"options":[[wpv-view name="your-users-list-view"]] 
}
[/cred_generic_field]

2 - is it true that if I turn off expert mode this field will get deleted? Is there a way to add it to the field group?
Yes that is accurate, and for now there is no easy way to add this generic field to the drag-and-drop form builder once you have switched into expert mode once. You must use expert mode and keep it active to retain your custom fields.

#1377517

So, since this field is not in the group, how can I filter by it in a view? For example, how can I filter by all posts assigned to that user?

#1377521

For example, how can I filter by all posts assigned to that user?
Great question. How do you plan to connect the User and the post, technically speaking? In general, there are two ways to connect Users and posts. You can make the User the author of the post, but that only allows one User to be connected to each post and there is currently no front-end post author filtering available in Views. So that's out.

You can store the User's ID in a custom field on the post, but then filtering by a User ID isn't really practical on the front-end unless your Users know everyone's numeric ID. I'm also guessing that's out.

This leaves a third way, which is more complex to set up initially but allows for more flexibility in the long run. This creates an indirect association between Users and posts by use of a proxy post type:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users

This would allow you to set up a front-end post relationship filter, which would allow you to filter based on legible User name instead of ID, and would also allow more than one User to be associated to the same post. This third option probably requires you to rethink the Form you already have created, since now you need to relate posts in a post relationship instead of relating posts and Users.

#1377577

So, I created a Contractors & Showcase CPT. In both I added a 'User ID' field. What I don't see is a way to map User Meta to those CPTs. Also, I think this option over-complicates things because (I think) it would require the user to (also) generate a profile _after_ an account has already been created.

Perhaps I need to simplify things a bit? If I have the user info in the select field, and it is 'selected' and saved to the post. Why can't I simply search it like any other field? Is that because it saves it to user meta? What happens with that field? Maybe I'm not understanding the issue?

#1378105

Also, I think this option over-complicates things because (I think) it would require the user to (also) generate a profile _after_ an account has already been created.
You can either create it automatically using Forms and custom code, or you can set up a simple one-button Form that says something like "Create Profile". The documentation link I provided explains how you can display that button to Users who have not yet created a profile, and hide it from Users who have.

Here's a link to another ticket where the goal is to create the proxy post automatically when a User registers with Forms:
https://toolset.com/forums/topic/creating-a-custom-post-type-with-cred-and-user-registration-at-the-same-time/

In both I added a 'User ID' field. What I don't see is a way to map User Meta to those CPTs.
Right, you're only storing the User ID as a custom field value on the post. It's a simple numeric value, no direct link to any User profile. There is no custom field to select a User or Users, so you are effectively working around that limitation by storing a User ID reference in postmeta.

Why can't I simply search it like any other field?
You can, but again, it's just a number. On the front-end of the site, your visitors would see a filter that contains options like 1, 15, 28, 43, etc...numeric values that don't mean anything to them because no additional User information is stored in postmeta, only the User ID. There is no way to tell the filter control that this number corresponds to a User ID, and the User information should be displayed instead of the User ID number.

There is no direct way to associate Users and posts except as post author. That method only allows one User to be connected to each post, and there is no front-end custom search filter for post author. So your two other options are User ID reference or proxy post type. Unfortunately the complication is a product of the way WordPress is set up to allow (or not allow) relationships between Users and Posts easily.

#1378183

So I cannot use the field we created, take the selection, and search the results of that selection? IOW, say within the form a user selects (user) test1. Isn't that selection 'test1' added to the post in a custom field? If it isn't, can't we do that too? So, at the same time they select 'test1' we also, using a hidden text field or something, write their selection?

#1378195

Isn't that selection 'test1' added to the post in a custom field?
No, only the value of the selection is stored, not the text label. The value is a number in this case - the User ID. It sounds like what you're suggesting is that we copy the User name or login or something and store it in a custom field as well as the User ID in another field. In general, it's not a good idea to have related data duplicated in two tables like this, because they are prone to get out of sync later. The single source of truth should be the User table, and the User ID is unique to that table so it's not going to change. There are ways to change User display names and logins, so I wouldn't rely on that stored text string. If you'd like to do that, I can't recommend it. It's error prone down the road. But yes, it is technically possible to change the User ID field to store some other unique text string, and the filter would display those unique text strings instead of numbers.

#1378265

Sorry, I misunderstood. So the value, which is the user ID is stored. So can that be mapped 'automagically' in another field? For example, within the CPT I have a select field called USERNAME and it does a lookup against userid and presents the current name in the field?

Ce ticket est maintenant fermé. Si vous êtes un client de Toolset et que vous avez besoin d'aide, veuillez ouvrir un nouveau ticket d'assistance