User Dropdown List & Custom Notification Routing in Gravity Forms

detourRecently I had to customize a form for a client using Gravity Forms. The basic concept was creating a single form with a drop-down to select the user the form would email. I figured there would be a plugin or built-in option for this, but alas I was disappointed. So let’s build it!

There is a “Configure Routing” option in the notifications and for a simple form you can add various conditional elements. For example, you can say if “Josh” is selected in the “Email to” field, send to a specific email address. However, this has to be manually entered, both the conditional statement and the send-to email address. I needed the ability to auto-populate the drop-down with WordPress users and send the notification (in my case a contact form message) to that selected user. Luckily, there’s a filter that we can use.

Populating the Drop-down with Users

First, be sure you have a drop-down field created on your form. An important step is creating a custom CSS class for the drop-down field, that way we can find the correct drop-down to populate (in the “Advanced” tab of the field options). For this example I’ve given the field class the identifier “user-emails.”

Next, add this to your functions.php file (or, if you’re being really future-proof place it in a plugin file).

<?php
/**
* Populate the drop-down menu with users
*
* @author Joshua David Nelson, [email protected]
**/
// 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;
}

In this example our form ID is 1, but change it in the filter name to match your form ID. Also, change the “user-emails” CSS class to whatever class your drop-down field is using.

What this code does:

  1. It takes the $form array and goes through each field in the foreach statement.
  2. Then we place a if statement to make sure we find the field we are looking for – a select field with a specific CSS class.
  3. After that, we build a new array of $choices, each with a ‘text’ and ‘value’ for the field. In this case, we’re using user data, so…
  4. Create a new WP_User_Query to find all the users. We’re only using the user id, display name, and the user email.
  5. Add the user display name for the text and the user id for the value.
  6. Set the field’s choices with our new array and return the $form.

Some things to note:

  • Why not use the email address for the value and pass that into the notification function (below)? Well, part of the reason I created this in the first place was to hide email addresses on the front end. Plus, it is easier to use the user id when utilizing the populate dynamically feature.
  • You can use other methods to filter the users in the initial query. If you are looking to only have, say, the contributors or admins. I’d recommend using the wonderful GenerateWP tool. If you do this, you can likely remove the && user_can( $user->id, 'edit_posts' ) conditional.

Using that Email for the Notification Send To Field

The next step is to use that field’s value (user id) and generate the notification’s send to email address. This gets added to your functions.php (or plugin) file:

<?php
/**
* Change the sent to email address in the notification
*
* @author Joshua David Nelson, [email protected]
**/
// 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;
}

Again, change the 1 in the filter name to your form ID and the CSS class like above.

What this code does:

  1. We’re filtering the notification here, and we’ve been given the $notification, $form and $entry arrays. The first two should be straight forward by their names, the $entry is the submitted values of your form.
  2. We’ll loop through the form’s fields and find the one we want (similar to the previous function).
  3. Once we find the right field, we grab the field id and use the $entry array to get the value selected (in this case, the user id).
  4. Using that user id and get_the_author_meta we’ll find the user_email we need.
  5. Set the notification ‘to’ field and return the notification.

That’s pretty straight-forward and basically it.

Wrap it up and Resources

You can view the entire gist here. Some valuable resources for this little project:

Hope that was helpful! It is pretty slick once it’s working.

Discussion

  1. I’m wondering. Could this be leveraged for sending an email to a specific email address based on the answers to the questions? That is, “I have a problem with my order” is selected in the dropdown so that email should go to the ‘orders’ email address?

    • Christy,

      I believe the functionality you’re looking for can be achieved with the built-in conditional notification options in gravity forms. You can set multiple conditional options, like if the value of a field is “I have a problem…” then send to this X email address.

      The method I outline in this post expands that functionality by dynamically populating a field (with users) and routing the notification based on that field. Gravity Forms will do this statically as I noted above, which sounds more of what you’re trying to achieve.

      Hope that helps!
      Joshua

  2. Thank you so much! This is perfect for what I was trying to achieve, which happens to be very similar to what you described with dynamically populating the users. Using the built in email routing just wasn’t feasible because of the number of rules I would’ve had to input – and on top of that I have 3 forms needing the exact same routing rules. Now info is pulled from custom post types – updating the post will propagate changes to all of the forms on the site!

  3. This post came up in a Google search, but merits an ‘almost there!’ response =)

    I’m trying to create a drop-down box in GF for the home page (Please select your location…), which redirects to that location’s page…. How would you do that?

    • Chris,

      That sounds like a relatively simple item – using Gravity Forms would be like using a sledgehammer when a chisel would do. I would recommend building a WordPress menu, cycle through the menu and use some lite javascripting (like this). There are plenty of examples out there on the interweb.

      Cheers,
      Joshua

  4. Hi Joshua, This is great and an awesome starting point,

    I am trying to send a submitted form to a custom user group or role group, BUT here comes the catch – I have 2 drop downs which consist of state/province and service category – Here is a scenario of what I’d ultimately like GF to do,

    If “state/provice” is “Western Cape” and “service category” is “painting” then send this enquiry to user group “Western-cape-painting”

    I have created the custom role, but i’m battling to find a filter which will achieve this.

    Any assistance or insight would be greatly appreciated.
    Thanks all the way from Cape Town, South Africa

    • Gregg,

      I could see how that would be cumbersome to enter by hand on the GF conditional routing interface, however that is one way to do it that is built-in (set up a list of conditionals in the notification settings).

      However, if you’d rather have it a bit more automated, I think you would want to utilize the second part of this post – the custom user notification routing. You’d want to populate a list (or pull the list from your fields) and route it based on the field values, something like $notification['to'] = $state . '-' . $service_category;. Obviously you’d want to generate those variables based on the $entry and $form variables.

      Does that help?

      Cheers,
      Joshua

      • Hi Joshua, Yes and no 🙂 although I will try it out and revert back 🙂
        Thanks a million.

  5. Sorta on this topic sorry to hijack it somewhere else a bit. But curious if you know of a way to mask the data in the notification email? lets say I want o indicate data has been submitted but not disclose it in the email itself. But still maintain the full record in the entry via WP login.

    Example:
    Input > Phone: (555) 555-5555

    Becomes
    Phone: (XXX) XXX-XXXX just in the notification email… not during input or on the admin side after logging in.

    I know I could customize the HTML in the notification. But I want to use the {all_fields} to avoid consistently updating the HTML as elements are added/deleted.

    • Craig,

      You’d probably want to hook into the gform_notification action in a very similar manner as I did in this post. Then, find the field you want (either using a custom css class value or the field label) and alter it. I’m pretty sure this is fired after the entry is already saved so it would only alter the content sent, but you’d have to text that to be sure. Gravity Forms’ documentation is pretty good (but it could be better). Basically, you’d have to find the field id (sort of similar to this), then use that to update the entry object.

      Hope that helps,
      Joshua

  6. Hi there

    After much searching you must be the first one that has got close to what im looking to do. Following from what Gregg du Plessis was saying i to am looking for a similar bit of code. I am looking to build a local directory site. All the business will be stored in the data base with there IDs, names, email etc. I want to create a gravity form for getting direct quotes from the costumer to certain supplies depending on the area (ie Location) and services (ie Categories )they provide which will be selected from drop downs. After the form is submitted it will then send out a notification email to those relevant suppliers. Please could you help me in achieving this goal. I am not to great at coding but trying to learn fast. i am use the Templatic Directory theme. Is this possible and if so could you help please.

    Thank You

    Kind Regards

    • Michael,

      This sounds fairly similar to what I have outlined in the second code snippet of this post. You’ll utilize the gform_notification hook to alter the outgoing notification email address(es). It sounds like you would need to write a function that takes the area and services selection and returns a list of email addresses (it would grab this information you’ve stored in your database based on whatever criteria you have for that selection), then use those email address(es) for the notification. Something like this: https://gist.github.com/joshuadavidnelson/802811631a4e7ec0ed54 (note this code is untested, so use at your own risk and change as necessary)

      Hope that helps! If you need further help on this issue, I’m available for hire 😉

      Cheers,
      Joshua

  7. Hi Josh,

    Michael’s concern was a bit close to my concern but mine is this:
    I have 10 email lists and each list has 4 emails. The notification will be sent to the list depending on the city but the problem is if the same city is chosen by another person, the notification will be sent to the next list until it reaches email list 10. So new list every time the same city is chosen. Do you have an idea?

    • Joann,

      I’m not entirely sure I understand the functionality you’re looking for – you have 10 lists with 4 potential notification recipients each and the form needs to route the notification based on the city chosen by the user submitting the form or a recipient value? I’d be happy to discuss this more and perhaps build a solution for you. It sounds very unique and complex! Drop me a line via my contact form if you’d like to talk more.

      Thanks,
      Joshua

  8. Dear Joshua, Thanks for this post. I think this is exactly the kind of post we were hunting for since past few days.
    I use configuration routing option of gravity form to route emails to specific ids based on one particular field value. However I need to use a combination of field values like “company name” is “Airtel” and region is “West” and industry is “Telco” then send to [email protected] (just an example). Is this achievable using the code you have suggested? I am not much of a programmer but can dabble a bit.

    Secondly, I have over 5 thousand such routing rules to update and maybe more – the content is the same for all. Can gravity form configuration routing support such large numbers in a single notification (When we touched about 200 entries, we found that the notification page stopped loading) and we opened a new notification with the same content for the next 200 entries. Is this a good option.

    Lastly (and apologies for pumping so many questions at once) is there an easier way to load these combinations of routing directly into the database rather than enter one by one, which is so time consuming.

    Thanks in advance!

    • Meera,

      That sounds very complicated indeed! I think handling the routing programmatically will definitely reduce the complication. Instead of building 5 thousand notifications, you could (potentially) build it in a custom function that would route it the way you described.

      You could use the code in this post as a start block, for sure, and pick other fields (be sure they are required fields) to build an email address like you described. Assuming all combinations of email addresses exist, it would send the notification to the specific addresses. If there are combinations that don’t exist, you might try modifying the fields on the form editor side with conditional fields based on each. For instance, the region field might be conditional on the company name field – you could have multiple region fields, each specific to a company. That simplifies the back-end functionality and ensures that you aren’t building an email address combination that doesn’t exist.

      In terms of the large numbers, you might have to increase your php memory limit to avoid issues.

      Your last question – these are already in the database, all the notifications & routing rules inputted via gravity forms are saved in the database.

      I’d be happy to build a custom extension to address these issues for you, my availability at the moment is late March, early April. Send me some information if you’re interested and I can get you a quote.

      Hope that helps!

      Thanks,
      Joshua

  9. I am building a Gravity Form with ACF Gravity Form Addon. I am using a post field dropdown rather than a standard field. I have your code working properly with the standard field, wondering what the difference is with the post field in the code base?

  10. Thanks for this Josh – looks like exactly what I need. However, it doesn’t seem to work properly for me. I’m wondering if it’s because of an update to one of the pieces of the puzzle.

    Oddly, it doesn’t even show an error so sadly I’m not able to provide any other information other than it doesn’t work! It’s specifically gives an error when I have correctly identified the form ID and the field class. If I don’t use those, there is no issue (but obviously the function doesn’t work.)

    Are you able to shed any light on this? Much appreciated!

    • Tommy,

      I’m swamped with work at the moment and I actually just set up a site using this code that is working just fine. I’d need to see your code and have an understanding of what you’re doing before I could help, though.

      A couple of likely suspects:

      • It’s important that the gform_pre_render_1 filter is correct – replace the “1” with the form id.
      • Be sure it’s selecting the correct field, either by utilizing a custom class and checking for it (which this example does on line 16) or by another $field property like it’s id. This code also checks the field type to make sure it’s a “select” field, so if yours is different, that will matter
      • The same above goes for the gform_notification_# filter and function, as well as the field checks on line 14.
      • A handy way to debug something like this is to use the error log and log everything in each step. Using a helper function like this one, you can test and quickly narrow in on where it’s failing.

      Hope that helps!
      Joshua

      • Thanks for the reply! Having dug a bit more, I think it’s a memory issue. We have 10,000 users in our database, so I guess that’s too many to filter through…which makes sense. I might have to find another way to achieve what I’m looking for.

        Thanks again!

        • Tommy,

          Yes, that’s likely an issue! I’d recommend putting together a separate function that creates the list and be sure to use a transient. You might even have it fire on admin_init to limit the transient refresh on the front end. Then you can pull this list from that function.

          You might also try limiting the the results to ids and names in the query arguments to decrease the query time (also consider using WP_User_Query or WP_Query), then pull the user email address in the notification routing function via get_the_author_meta. Check out 10up’s PHP best practices for more on that.

          Hope that helps!
          Joshua

          p.s. Do these need to be actual users or could they be a custom post type with the user email stored in a meta field? That would limit the number of users with access to your site and could be quicker to load. I’m going to put together a post similar to this one with a walkthrough for that condition – at some point in the future.

          • Thanks again for the reply! We have a membership system / form and this particular part is to ‘gift’ a membership to another user.

            I’ll be honest…I have no idea how to do anything you suggested 🙂 so I might just use a text field and they can type in their username if they know it.

            Thanks!

          • Tommy,

            That might be a better route given you circumstances, and also a bit more of a privacy safeguard. You could also set up a email notification to route to the user who submitted it, so if the username they entered doesn’t existing they receive a notification saying their message failed.

            Cheers,
            Joshua

  11. Just want to say thanks! This was more useful than any of the results from the GF website even though all I really needed was confirmation that the built-in features would work!

    Thanks especially for the links to the docs at the bottom, GF is terrible at making those docs findable, I always end up on their marketing pages :S

  12. Hi
    I came across your post in search of a similar function for a site im looking to build.

    Is it possible to have the drop down populate with a ACF custom field of a list of store names. Then when that store is selected it adds the ACF custom email from that store as a routing option to also receive the notification email.

    Many thanks
    Ryan

    • Ryan,

      I think this is definitely doable. You’d have to pull the ACF field value after confirming the correct field and loop through the value to populate the list, similar to above where I pull the users information. Similarly for the notification portion.

      Best of luck!
      Joshua

      • Hi Joshua,

        Thanks for the reply. I’ve been tinkering with your notification code but cant get my head around it.

        Is this what you mean?

        foreach( $form['fields'] as &$field ) {

        // Similar to above, find the right field
        if( $field['type'] != 'select' || strpos($field['cssClass'], 'studio-list') === 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 );
        $email_to = get_post_meta($post->ID, 'studio_email_1', true);
        }

        if ( !empty( $email_to ) && is_email( $email_to ) ) {
        $notification[ 'to' ] = $email_to;
        }

        return $notification;

        • Ryan,

          I’m not 100% sure exactly what you’re trying to do, but here’s a swing at it…

          Assuming that the studio-list field is a select field and the value is a post id (not a user id, as the variable is named – that’s likely just copied from the above example, right?). If that’s right, then you would change the $post->ID in your $email_to = get_post_meta($post->ID, 'studio_email_1', true); to use the $user_id value taken from the form field.

          Something like this gist.

          Hope that helps!
          Joshua

          p.s. In the future, please use a gist for code.

  13. What would be the most efficient way of inserting a block of text above the email that gets sent to the email address tied to the username when selected from the “user-emails” dropdown field?

    • Hunter,

      All you want to do is add text in the email sent? If it’s the same in each case, you can add that in the notification settings themselves. This code simply routes the notification to the user selected. If there is no notification set up, then nothing will happen.

      Go to your Form Settings > Notifications and edit the notification(s) related to the form.

      For a slightly more complex example: the form I originally set this up on had another field that set the inquiry type. I had four notifications in the form settings, each tied to conditionally send based on the inquiry type field. The re-routing code only came into play if inquiry type was set to “employee contact” and a user was selected. Note that the above code will route the notification if the user is set, regardless of any other field or notification settings. If you want to make it conditional on a field like I just mentioned, you’ll need to add code to check the inquiry time as well as the user email validity.

      Hope that helps!
      Joshua

  14. Hey Josh…
    I do believe that this is going to answer a prayer! I have a single form that I want to dynamically populate a dropdown field with the WP user names. I have approximately 500 users and I’m wanted to select one of those and then add work history with other dropdown fields. Wanting to be able make this user field the common field among all of the work history entries. Am I on the right track?

    I’m not extremely proficient with coding so I thought that I’d ask before I went down the road of disappointment. 🙂 Can you give me a quick rundown of what I need to add or leave out of this example to get it working?

    • Tim,

      This example would populate the dropdown field with users. However, if you’re linking another dynamic dropdown to said user field that’s beyond the scope of this walkthrough (it sounds like you want to pick a user, then have the next field populate based on what user was chosen, right?). You’d need to utilize either ajax and get a bit deeper into GF, might be a task for a hired developer if you’re not familiar.

      If you’re just looking to dynamically populate a dropdown field (not linked to another one), you can populate as many as you like with this method.

      Hope that helps!
      Joshua

      • Thanks for the super quick reply… No, I do not want to dynamically populate the other fields…. just the one user field. The reason for that is so that I can add work history by selecting a user and then choosing the event and work position that filled for that event.

        Very simple, (I think)… So just to clarify:
        I don’t need the second part… just the first. Right?

        Also, what do I put in the “Parameter Name” field of the form?

        Is there anything else that I have to do other than making sure my form numbers are changed in the code?

        Gonna try this out and see if I can pull it off, but wanted clarify these few things while you were available! 🙂 Thanks again!

      • When I place this code into my functions.php file, i get the ole “white screen of death”. Tried several things… but nothing. Help please? Any idea what am I missing?

      • Hey Joshua…
        I have successfully gotten this to work! Thank you! One thing that I need help with. This is showing the display_name in the dropdown and then only passing the user_id to the form… Which is exactly what I need! But, I also need a field (hidden preferably) that will match up the same user’s display_name so I can display a “name” and not just a number. Again, thank you so much! This has really been a blessing to this project I’m working on!

        • I’m confused, do you want to change what it’s using from user id to display name, or are you trying to use the display name somewhere else? You can always use the user id to find the display name via get_userdata.

  15. Hi Joshua. Thanks for the code. This is what i needed!! One more thing, is it possible to send the form record (for example, when member1 selected, record shall be pass to member1 page) after the record send to member1’s email address? I am running a membership site and hopefully you can help me with this. This form can only be submitted by Admin only. The record shows to admin & all user. I would like to show the specific records to specific selected member as per done in your code here. Im using gf list & edit plugin but it only works on form submitted by member1 from its own page only. Thanks a mill!!

    • Glad it was helpful for you! In regards to your inquiry, I’m not entirely sure what you’re looking for in terms of functionality. Are you trying to send the notification to multiple email addresses, or send a separate notification to a specific address? You can easily do the latter with the built-in GF notification options, just add another notification and have it route to the submitted email address. You could modify the code above to use the selected email address form the field (which you can pull from the $entry object).

  16. Thanks for this info- really helpful!

    I am trying to set up a gravity form that allows logged in users to send messages to each other. I’d like them to be able to select user using checkboxes, so that sending a message to more than one user is possible, and have modified your code to work with a checkbox field- but I think your code is set up to send only to one email address, correct? How do I modify the code to send to all addressees selected in my checkboxes? Here’s what I have added to my functions.php file:

    https://gist.github.com/raff2145/2a630bb89039e56a1545f521ead0d4c5

    • Sean,

      Awesome to hear this code was helpful!

      In terms of pulling multiple users from a checklist, I would suggest placing the checked users into an array, then looping through that array to build a string of email addresses for the notification ‘to’ field. Now, the downside to this method is that all the users will be in a group email together, which I’m guessing is not ideal. I haven’t used it myself, but you should be able to add the comma-delineated list of users to $notification['bcc'].

      I have not tested this, but you’re probably looking for something like this code: https://gist.github.com/joshuadavidnelson/6e434be4d9264775738d7da8f225ff74

      Hope that helps, sorry I didn’t have more time to fully test this, but hope it gets you in the right direction.

      Cheers,
      Joshua

  17. Hi Joshua,

    I was searching for a similar solution to my site. I build a business directory site, and would like to develop a solution for this:

    When a visitor fills out the enquiry form (Gravity Forms), the submitted content goes to several listings at the same time (bcc).

    Listings are stored as custom post type, they also have the custom post type taxonomy.

    I would route the emails to be sent to particular listing categories, selected by radio buttons (http://palyazatirok.palyazatmenedzser.hu/ajanlatkeres/).

    Can you help me with this?

    Thanks in advance,

    Richard

    • Richard,

      You can use the $notification['bcc'] parameter to route notifications to additional email addresses (in a comma-delimited list), and grab the values to your radio buttons to determine where it needs to be routed. Hope that helps get you on the right path.

      Unfortunately, this is a bit beyond the scope of this post. I do provide custom development services, with some availability later next month. Reach out on my contact form or email me: josh [at] this domain.

      Thanks,
      Joshua

  18. I’m trying to get this to work with one of my forms, but the email portion is not working. What am I supposed to do to the notification settings on the form? I had it configured with routing, but I want the code to handle the email address.

    • Bryan,

      The second portion of this post details how to override the notification routing – which it does programmatically. The settings for the notification routing shouldn’t matter because the code above will overwrite the “to” field. However, I’ve typically only used this with the default “send to email”, not the “configure routing” setting (because the code does that part), so perhaps reverting that setting will fix your issue.

      If you have multiple notifications, you could expand on the code above to check for a specific notification (see the Gravity Forms Documentation for more info). It’s possible that you have multiple notifications and that’s causing a conflict – I don’t think that would be the case, but I can’t recall now if I’ve used this on forms with more than on notification set up.

      Hopefully, that helps but let me know if you have more questions.

      Cheers,
      Joshua

      • Thanks Josh,

        I only have one notification, and like I said….I was manually configuring the routing on it. If I am to have a notification configured, I have to place “something” for the email “field” or manually enter an email address, OR configure routing.

      • Hey man. I got it working, and it works great. One more question:
        What if I want to use this on more than one dropdown in a different form? I know I would have to change id’s etc, but what else would I need to do? I’ve tried changing variable names, and I just can’t seem to get it.

        Thanks….cheers…

        • Bryan,

          Glad to hear! You could run another filter or you could run a single function on the gform_pre_render filter for all the forms, checking for the form id and unique cssClass for each dropdown (similar to line 16 in the populate code above, which checks for a specific class, you’d need to modify this slightly and have unique classes for each dropdown or use their ids). Check out the example in the Gravity Form documentation here.

          Hope that helps!

          Thanks,
          Joshua

          • Right on. I’m trying to filter role:
            $args = array(
            ‘role’ => array( ‘buyer’, ‘rpc_system’ ),
            // order results by display_name
            ‘orderby’ => ‘display_name’,
            // Return the fields we desire
            ‘fields’ => array( ‘id’, ‘display_name’, ‘user_email’ ),
            );

  19. Hey Joshua, this is great code and has got me almost to where I need to be for my project.

    I’m having a similar issue to Tim above . When a user is selected from the dropdown, the merge tag (for email content) normally functions with {[Field Name]:[field_id]} returning the “text” and {[Field Name]:[field_id]:value} returning the “value” or a dropdown. But using this code, both the standard and modified merge tags for the dropdown values are set to the user_id.

    I would like to be able to use the display_name of the user as a merge value in the email content. The context being an email will be sent to the selected user, and notification to another user with the display_name of the selected user. I suspect this is straight-forward, with the right understanding!

    Can you please point me in the right direction?

    • Luke,

      Thanks for the comment!

      The code above should be setting the ‘text’ to display name and the ‘value’ to the user id (line 43-46 in populate-user-email-drop-down.php). However, the merge tag for {[Field Name]:[field_id]} returns the value (per the Gravity Forms documentation), so in this case it will always end up being the user id.

      That said, there is a filter available for merge tags, so you could filter the value and use get_userdata to replace the user id with the display name for the merge tag.

      Hope that helps!
      Joshua

      • Perfect! Got it working (learned a fair bit on my travels!).
        Thanks for the tips!