Tell us what you are trying to do?
So i have an existing site that utilizes custom fields that i'm trying to migrate over to Toolset. I've succeeded in moving the majority of the old custom fields over by exporting them to CSV, re-importing them and then linking them to the new custom fields i created in Toolset via their slugs. One field is giving me trouble though, which is a dropdown field (that exists on the original site) that shows all users so that you can link the 'Member' (a custom post) to an Associated User. I've attached an image on how it works/looks on the old site, and in the CSV export of that custom field it gives me a numerical value which i assume is the user ID (usually something between 10-900). Unfortunately i'm not sure how to implement that into the Toolset Dropdown option (also attached) where i can only input Display Text and Custom Field content. Is there a way to replicate this?
Is there any documentation that you are following?
Not currently.
Is there a similar example that we can see?
The only existing example i have is what the old site uses, which is in the image attached (names blurred for privacy)
What is the link to your site?
The site is hosted locally so i have no link.
Hello and thank you for contacting the Toolset support.
To pull the options of a select field from the database, be it users, custom posts, or other content, we'll need to hook into Toolset and fill the options with custom code. Check this previous ticket that is trying to pull data from a custom post type.
https://toolset.com/forums/topic/add-select-field-to-users-from-a-custom-post-data/
add_filter('option_wpcf-fields', 'fill_my_users');
function fill_my_users($fields) {
foreach ($fields as &$field) {
if ($field['id'] == 'your-field-slug') {
$field["data"]["options"] = array();
$users = get_users();
foreach ($users as $user) {
$key = $field["id"] . $user->user_login;
$field["data"]["options"][$key] = array(
"title" => $user->user_login,
"value" => $user->ID
);
}
}
}
return $fields;
}
I hope this helps. Let me know if you have any questions.
Hi Jamal,
Thanks so much for the quick response, i'm sorry i didnt see this sooner.
I understand in theory what you mean, unfortunately i'm a bit confused though as to how to 'hook into Toolset' and work with the code you provided though.
I've read through the other thread you linked, and it does seem to be a very similar situation which is great, i'm just a bit lost in certain areas. Where he says "You can use the wpt_field_options filter to dynamically fill a select field's options." i'm not sure how that ties in, is there some sort of code section within the wordpress/toolset local files that i need to edit or something?
Also i noticed your code is slightly different than the template provided in that other thread, which i assume is because you already changed the default options to fit in perfectly with what i was asking for, in which case i would only have to modify the "your-field-slug" part with whatever slug i create in the new custom field for the selection dropdown?
Thanks again, sorry for the trouble as a bit of this is going over my head.
Hello! It is alright. When I said hooking into Toolset, I meant the WordPress way of hooking into the execution process. And it is done with one of the following function add_filter and add_action. That's WordPress way of hooking into WordPress and plugins without having to modify their code.
You MUST not modify WordPress or Toolset code. Read more about hooking into WordPress and Toolset in these articles:
- https://developer.wordpress.org/plugins/hooks/
- https://toolset.com/documentation/programmer-reference/toolset-hooks/
The provided code is, indeed, almost ready, you will have to change the field slug with your actual field slug. The first line of the code is hooking(executing) the function(from 2nd line to the end) when Toolset is filtering "option_wpcf-fields". You will also need to test it manually because I did not test it on an actual installation.
Basically, you just need to add the code I provided on your theme's functions.php.
Let me know if it does not work for you and I'll help with it.
Hi Jamal,
Thanks so much for all the help, i think everything is working now. I just wanted to double check something though, i modified the code a bit so that rather than displaying user logins it displays the user's first name/last name, so it reads:
title" => $user->first_name . ' ' . $user->last_name
I just wanted to check if that's the best way to go about it in case there are any unforeseen consequences, or whether there might be a better/simpler way to do it.
Thanks again.
Hello! That's great!
I think the sample code is fine and will concatenate the first name and the last name with a space in between, just make sure to have the first double-quote(") and the last comma (,)
"title" => $user->first_name . ' ' . $user->last_name,
All the best!
My issue is resolved now. Thank you!