Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active November 6, 2020 16:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshuadavidnelson/b9624760a81c2c35f6b5 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/b9624760a81c2c35f6b5 to your computer and use it in GitHub Desktop.
User Dropdown List and Custom Notification Routing in Gravity Forms. See the walkthrough:
<?php
/**
* Add the file uploads to the emails
*
* @author Joshua David Nelson, josh@joshuadnelson.com
*
* @link http://www.gravityhelp.com/documentation/page/Gform_user_notification_attachments
* @link http://www.gravityhelp.com/documentation/page/Gform_notification
**/
add_filter( 'gform_notification_1', 'jdn_add_email_attachments', 10, 3 );
function jdn_add_email_attachments( $notification, $form, $entry ) {
$fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));
if( !is_array( $fileupload_fields ) )
return $notification;
$attachments = array();
$upload_root = RGFormsModel::get_upload_root();
foreach( $fileupload_fields as $field ){
$url = $entry[ $field["id"] ];
$attachment = preg_replace('|^(.*?)/gravity_forms/|', $upload_root, $url);
if( $attachment ){
$attachments[] = $attachment;
}
}
$notification["attachments"] = $attachments;
return $notification;
}
<?php
/**
* Change the sent to email address in the notification
*
* @author Joshua David Nelson, josh@joshuadnelson.com
**/
// Route to user address from drop down list, update the '1' to the ID of your form
add_filter( 'gform_notification_1', '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'] != 'select' || 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;
}
<?php
/**
* Populate the drop-down menu with users
*
* @author Joshua David Nelson, josh@joshuadnelson.com
**/
// Gravity Forms User Populate, update the '1' to the ID of your form
add_filter( 'gform_pre_render_1', '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 dropdown 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'] !== 'select' || strpos($field['cssClass'], 'user-emails') === false )
continue;
// The first, "select" option
$choices = array( array( 'text' => 'Select a User', 'value' => ' ' ) );
// 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 can be imported without email addresses
// Also, make sure the user is at least able to edit posts (i.e., not a subscriber). Look at: http://codex.wordpress.org/Roles_and_Capabilities for more ideas
if( !empty( $user->user_email ) && user_can( $user->id, 'edit_posts' ) ) {
// add users to select options
$choices[] = array(
'text' => $user->display_name,
'value' => $user->id,
);
}
}
}
$field['choices'] = $choices;
}
return $form;
}
@USTSweb
Copy link

USTSweb commented May 23, 2019

Many thanks, it really helps <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment