~I would like to be able to populate a drop down box with a list of all usernames
Possible?
~I would like to be able to populate a drop down box with a list of all usernames
Possible?
Here's the code I'm working on in functions.php
function populate_userdrop($form){
//only populating drop down for form id 1
if($form["id"] != 1)
return $form;
//Creating item array.
$items = array();
// Get the custom field values stored in the array
$meta = get_users();
if (is_array($metas))
{
foreach($metas as $meta) $items[] = array("value" => $meta->display_name, "text" => $meta->display_name);
}
//Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 1){
$field["choices"] = $items;
}
return $form;
}
All I've done on the form is allowed it to be dynamically populated.
Here's the form in action - http://ethicalincubator.com/BMentor/form-test/
Wait, did you figure this out? On your form, I see a dropdown with user names in it.
I AM STOOPID. After debugging I realised I missed the 's' of 'metas'.
Thanks for the reply. Here is help for anyone else trying this.
Here's an example of my fully working code to populate a dropdown box in Gravity forms with a list of user display names.
Add this to functions.php
// This adds display names for all users to a drop down box on a gravity form.
add_filter("gform_pre_render", "populate_userdrop");
//Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter("gform_admin_pre_render", "populate_userdrop");
function populate_userdrop($form){
//only populating drop down for form id 1 - if editing this change to your own form ID
if($form["id"] != 1)
return $form;
//Creating item array.
$items = array();
// Get the custom field values stored in the array
// If editing this lookup where you would like to get your data from
// this example loads through all users of the website
$metas = get_users();
if (is_array($metas))
{
// in this example we just load the display_name for each user into our drop-down field
foreach($metas as $meta) $items[] = array("value" => $meta->display_name, "text" => $meta->display_name);
}
//Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 1){
$field["choices"] = $items;
}
return $form;
}
Yeah, I saw your if statement and was like "where did he define metas?" Glad you got it sorted.
Thank you chrishill123, this exactly what i was looking for!