Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rupomkhondaker/4fd7a4a666a1897ad20b0eddaa84edf1 to your computer and use it in GitHub Desktop.
Save rupomkhondaker/4fd7a4a666a1897ad20b0eddaa84edf1 to your computer and use it in GitHub Desktop.
Demonstrates a plugin for using database values to populate a dropdown control in Participants database.
<?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
*/
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
/*
* 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