Demonstrates a plugin for using database values to populate a dropdown control in Participants database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB Volunteer Dropdown | |
* Description: load the registered volunteers into a Participants Database form dropdown | |
*/ | |
/* | |
* sets our function to be called when the pdb-form_element_build_dropdown action | |
* is triggered by the form | |
* | |
* if your field is not a dropdown, change the action so it is triggered on the correct | |
* element, for example if the field is a multiselect checkbox, the action would be | |
* 'pdb-form_element_build_multi-checkbox' | |
*/ | |
add_action( 'pdb-form_element_build_dropdown', 'xnau_set_volunteer_dropdown_options'); | |
/** | |
* sets the options for the volunteer dropdown | |
* | |
* @param PDb_FormElement object $field the current field | |
*/ | |
function xnau_set_volunteer_dropdown_options ( $field ) | |
{ | |
if ( $field->name === 'volunteers' ) : // check for our dropdown field | |
global $wpdb; // grab the db helper object | |
/* | |
* For multiselect fields, values that don't match the defined values for the | |
* field are dumped into the "other" element of the value array. In this case, | |
* we take the "other" value and make it into an array and add it to the main | |
* array of values so that they will show as selected in the form element. | |
*/ | |
if ( $field->is_multi( $field->form_element ) && isset( $field->value['other'] ) ) { | |
$field->value = array_merge( (array) $field->value, explode(',', $field->value['other'] ) ); | |
} | |
/* | |
* define the query for getting the list of volunteer names | |
* | |
* note that the $wpdb->prefix method is used to get the table | |
* prefix; this is so it will work on all WP installs | |
*/ | |
$query = ' | |
SELECT first_name,last_name | |
FROM `' . $wpdb->prefix . 'participants_database` | |
WHERE form_type LIKE "%volunteer%" AND form_type NOT LIKE "%coordinator%" | |
'; | |
// now execute the query and get the results | |
$raw_names = $wpdb->get_results( $query ); | |
/* | |
* now expand the result array into an array for the options property of the | |
* dropdown | |
*/ | |
$options = array(); | |
foreach ( $raw_names as $record ) { | |
$options[] = $record->first_name . ' ' . $record->last_name; | |
} | |
// now set the field object with the new options list | |
$field->options = $options; | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To add separate titles to the options, place the title in the key of the option in the options array...like in this example:
$options[ ucwords( $record->first_name . ' ' . $record->last_name ) ] = $record->first_name . ' ' . $record->last_name;