Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple User Gist
<?php
// =============================================================================
//
// CUSTOM PLUGIN SUPPORT FUNCTIONS
//
// =============================================================================
// Populate user display name options for GF form ID 3
// in the drop-down field identified with class "user-emails"
// -----------------------------------------------------------------------------
// from: Populate the drop-down menu with users
// @author Joshua David Nelson, josh@joshuadnelson.com
// site: https://joshuadnelson.com/user-dropdown-list-custom-notification-routing-gravity-forms/
// -----------------------------------------------------------------------------
// Step 1: Populate users in gform_pre-render_#, update # to the ID of your form
// -----------------------------------------------------------------------------
add_filter( 'gform_pre_render_3', 'populate_user_email_list' );
function populate_user_email_list( $form ){
// Add filter to fields, populate the list
foreach( $form['fields'] as &$field ) {
// If the field is not a checkbox and not the specific class, move onto the next one
// This acts as a quick means to filter arguments until we find the one we want
if( $field['type'] !== 'checkbox' || strpos($field['cssClass'], 'user-emails') === false )
continue;
// Collect user information
// prepare arguments
$args = array(
// order results by user_nicename
'orderby' => 'user_nicename',
// Return the fields we desire
'fields' => array( 'id', 'display_name', 'user_email' ),
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
// Get the results
$users = $wp_user_query->get_results();
// print_r( $users );
// Check for results
if ( !empty( $users ) ) {
foreach ( $users as $user ){
// Make sure the user has an email address, safeguard against users being imported without email addresses
if( !empty( $user->user_email ) ) {
// add users to select options
$choices[] = array(
'text' => $user->display_name,
'value' => $user->id,
);
}
}
}
$field['choices'] = $choices;
}
return $form;
}
// -----------------------------------------------------------------------------
// Step 2: Now match up the user ids above with their send-to email addresses
// -----------------------------------------------------------------------------
add_filter( 'gform_notification_3', 'route_user_email_notification', 10, 3 );
function route_user_email_notification( $notification, $form , $entry ) {
foreach( $form['fields'] as &$field ) {
// Similar to above, find the right field
if( $field['type'] != 'checkbox' || strpos($field['cssClass'], 'user-emails') === false )
continue;
// Pull out the user id selected, by the field id and the $entry element
$field_id = (string) $field['id'];
$user_id = $entry[ $field_id ];
}
// set the email address to send the email to
if( !empty( $user_id ) ) {
$email_to = get_the_author_meta( 'user_email', $user_id );
}
if ( !empty( $email_to ) && is_email( $email_to ) ) {
$notification[ 'to' ] = $email_to;
}
return $notification;
}
// =============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.