Make no mistake about it, Formidable Pro is my favorite form building tool. Why? One of the primary reasons is the unparalleled flexibility provided by its API. Formidable Pro offers developers many hooks that allow you to easily take advantage of built-in core WordPress functionality.
To illustrate, in the exemplar below I am accessing the $wpdb object to populate a Formidable Pro dropdown with custom values pulled from a third party membership management table. The requirement is from a form built for the North Carolina Executive Roundtable (NCER). The North Carolina Executive Roundtable is a professional association of senior-level executives located in Raleigh, NC. Membership is by invitation only. The site's forms require members to submit their name. Of course, there are several ways to do this. We could have members directly enter their name in a text field, which can lead to inconsistencies later on if generating reports, or we could have members select their name from a drop down built from membership details.
Extracting membership details may not be an easy task however. Members come and go and memberships expire if dues aren't paid. As a result, the membership list is dynamic, which means any dropdown built from a membership tables needs to be dynamic. Membership plugins like S2member use the wp_users table to store membership details. Additional attributes other than the core user information is stored in wp_user_meta, which requires a SQL join to extract all appropriate details. Other plugins like WP eMember from Tips and Tricks HQ use their own tables to store member details.
In the former situation, it's appropriate to access the user details with WP User Query. In the latter, the NCER website uses WP eMember as its membership management tool, so it is appropriate to use the $wpdb object. To make this all work with Formidable Pro, we'll be accessing the frm_setup_new_fields_vars and frm_setup_edit_fields_vars hooks.
To get started, copy the code below to your functions.php file. Change the SQL in the line 10 that begins with $the_Result with your own custom query. Since you are using custom code, make sure you've tested it thoroughly in PhpMyAdmin, MySQL Workbench or another of your favorite SQL tools. Make sure you receive the expected results before adding the custom SQL query to your code. This is important because the Formidable Pro support agreement does not permit their excellent support team to help with custom code. So it is incumbent upon you to test, test, test and test again to make sure your custom code is right in every respect.
//populate the member dropdown field for nomination and member-advocate event forms
// data comes from wp_wp_eMember_members_tbl
add_filter(frm_setup_new_fields_vars, frm_populate_member, 20, 2);
add_filter(frm_setup_edit_fields_vars, frm_populate_member, 20, 2); //use this function on edit too
function frm_populate_member($values, $field){
if($field->id == 732 || $field->id == 733){ //replace 732 and 733 with the IDs of the fields to populate
global $wpdb;
//the Query
$the_Result = $wpdb->get_results( "SELECT concat_ws(', ', last_name, first_name) as display_name FROM wp_database.wp_wp_eMember_members_tbl where account_state = 'active' order by last_name, first_name;" );
unset($values['options']); //break the binding of any existing content in the values array
$values['options'] = array(); //remove this line if you are using a checkbox or radio button field
$values['options'][]="";
//the Loop
if(! empty($the_Result)){
foreach($the_Result as $a_user){
$values['options'][]=$a_user->display_name;
$values['use_key'] = false;
}
}
//$values['use_key'] = true; //this will set the field to save the post ID instead of post title
}
return $values;
}
If you run into issues, please feel free to contact me. In most cases, I'll be able to help solve your problem in a few minutes. If it takes longer than a quick fix, my consulting rates are reasonable.