Dynamically Populating User Role

Use this snippet in conjunction with Gravity Forms dynamic population functionality to populate the current user’s role into any form field.

This snippet assumes the “Allow field to be populated dynamically” option is checked and the Parameter Name option is set to “user_role”.

Dynamic Population Settings: User Login

Other Uses

Need to apply conditional logic based on the user role? Just use this snippet to populate a hidden field with the user’s role. You can then apply conditional logic to other fields based on the value of that hidden field. Read this related tutorial for more information.

Comments

    1. Dario Space
      Dario Space Staff May 6, 2021 at 3:38 pm

      Hi Gaston,

      WordPress does not (currently) have a global function (e.g. get_role_name()) for getting the role name (e.g. Shop Manager for shop-manager or maybe shop_manager etc.) but you could use GF Populate Anything to populate current user role name.

  1. Frank
    Frank October 26, 2019 at 9:08 am

    Hi there, populating this into a hidden field will allow users to edit this from the html source not? Also with administrative fields? Users can see it and edit it.

    Seems like quite a risk then to depend on this when using it in conditional logic for example?

    Reply
    1. David Smith
      David Smith Staff November 4, 2019 at 5:01 pm

      A fair point, Frank. Given the conditional logic fields are output to the page regardless, it is a negligible security risk as a user savvy enough to alter the value of a Hidden field would be savvy enough to view fields hidden via conditional logic. Use in feeds and notifications has a great chance of posing a security risk but the malicious user would need to be intimately familiar with how the specific form, feed and notifications were configured to exploit this effectively. Regardless, this is something we’ll take a deeper look at and see if we can’t tighten up.

  2. Soso Sadunashvili
    Soso Sadunashvili August 5, 2019 at 6:22 pm

    Hi Devid,

    Thank you for the nice and useful article, but Is there any way to populate fields for certain users, like this:

    I have registered user A and B, for registered user A I need to show field AA and for registered user B I need to show field BB.

    could you please suggest me any solution, I tried to search over the internet solution like this but can’t find…

    Thank you in advance.

    Reply
  3. daniel parrott
    daniel parrott November 23, 2018 at 4:11 am

    Hello

    I just want to say, I recently discovered this and it is one of the best things that exists! Now my forms can display different content to different users!!! It is amazing. thanks

    Reply
    1. David Smith
      David Smith Staff July 10, 2018 at 11:15 pm

      Hm, this is still working for me locally. I’m not aware of any changes in GF that would cause this to stop working.

  4. Carleigh
    Carleigh April 25, 2018 at 2:24 pm

    Hi David – thanks so much for this snippet. In my testing it seems to capture the user role upon creation of the entry, but if the user’s role changes the field is not updated. Is that correct? I am looking for a solution where the field can always reflect the user’s current role. I want to be able to use that field as a filter (in GravityView) so something is only displayed if the users role matches the filter.

    Reply
    1. David Smith
      David Smith Staff April 25, 2018 at 3:12 pm

      Hi Carleigh, we don’t have a solution for that one. Theoretically, you could use the profile_update WordPress hook to listen for updates to the user. It might be more bullet proof to listen on the update_user_metadata hook for changes to the user_role. Then you’d need to find all entry on the desired form that the user created and update the field with their new role.

    2. greentree99
      greentree99 June 25, 2018 at 10:28 am

      I’m also in need of Carleigh’s exact request. We’re using this in combination with the Update Entry on Submission snippet (https://gist.github.com/spivurno/32e914d67723f89717be2904ce6234c4). Sometimes we have two different users with different roles editing the same form. The idea is that when an entry is updated (as opposed to created), that Gravity would “rescan” the user role, and accordingly show/hide their conditional fields based on the current role, while keeping earlier hidden entries intact.

      Any ideas on how to customize this to get that working? Thanks very much

  5. Diego
    Diego April 16, 2018 at 5:05 pm

    Great Snippet! Tks a lot!

    Just one question…

    My users has multiples roles. Is there a way to bring all Roles of a user in a hidden field (or maybe in multiple hidden fields)?

    Reply
  6. Ken Carskadon
    Ken Carskadon June 2, 2017 at 4:59 pm

    I just tried using this and it didn’t work. I created a form with ID 1. In that form the first field is a single line text field. I checked “Allow field to be populated dynamically” and added the “user_role” parameter name. I then copied and pasted the filter into my functions.php file. I saved the php file, updated the form, and refreshed the page, but the field is still not populating with any information. I am not sure what else to do. I am currently working on a website locally using a WAMP server. I should also note that I have added the “gf_readonly” ready class to the field I am trying to populate.

    Do you have any advice for me?

    Reply
    1. David Smith
      David Smith Staff June 3, 2017 at 9:44 am

      Hi Ken, I’ve confirmed this is still working for me locally. There’s a chance that there is a conflict between this snippet and something else you’re running. A theme/plugin conflict test is always worth a try: https://www.gravityhelp.com/documentation/article/testing-for-a-themeplugin-conflict/

      If that doesn’t help, double check your configuration and step through any relevant steps here: https://gravitywiz.com/documentation/snippet-troubleshooting/

    1. heroj
      heroj May 5, 2017 at 9:00 am

      In this tutorial I must add tag “user_role”. So for another dynamically-populating action – where can I find another tags? Any list of this tags?

    2. David Smith
      David Smith Staff May 8, 2017 at 10:16 am

      This code is specific to the user role. You would need to modify the snippet to fetch other types of user data.

    3. heroj
      heroj May 10, 2017 at 12:32 pm

      Thank You for reply, and is possible use dynamic fields in e-mail notification? For example: “user_email” as sender email adress. Works it?

    4. David Smith
      David Smith Staff May 10, 2017 at 9:17 pm

      Best way to do this is to dynamically populate it into a field on the form. You can probably set the default value like so: {user:user_email}

  7. Schalk
    Schalk May 10, 2016 at 7:34 am

    Hi David, somehow related,

    How do I pass a url variable to Gravity Forms, even when the users navigate away from the landing page, before returning to submit the form.

    1.) Link: http://www.mysite/about/?affiliate=partnername – DONE 2.) Setcookie to remember parameter, even when users navigate around the site before returning to submit Gravity form ????? 3.) Add hidden fields to Gravity forms with parameter “affiliate” – DONE

    Reply
  8. Rob Snow
    Rob Snow April 14, 2016 at 4:02 am

    Hi Was talking to the guys at GravityForms,and they directed me here. It looks like this could help, but I am lost as a programmer. Can you tell me. 1) I want to have a conditional based on the user_role. I have installed a plugin that allows the creation of new roles i.e.: form_user. What I would like is a hidden field that places in something like {user:user_role} and so the conditional will check if this is in fact ‘form_user’ and then applies the condition. Is that possible? 2) If it is possible using this script, where do I add the code, and what changes would I make to allow that? Thanks in advance

    Reply
    1. David Smith
      David Smith Staff April 14, 2016 at 9:12 am

      This article demonstrates how to populate the user’s role. And this article demonstrates how you can apply conditional logic based on the value of the field you’ve populated with the user role. Be aware, the second article actually is about the user login; however, the same concept applies.

    2. Rob Snow
      Rob Snow May 14, 2016 at 2:35 am

      Hi Again I was talking to the guys at GravityForms on a different matter, but was wondering if the same thing could apply to the code for the PHP function.php addition. Would it still work if it was placed inside a mu-plugin created for this role? They are telling me to make one for a different reason, and as the function.php gets updated when the theme does a mu-plugin would be constant. Let me know. http://justintadlock.com/archives/2011/02/02/creating-a-custom-functions-plugin-for-end-users

    3. David Smith
      David Smith Staff May 14, 2016 at 12:28 pm

      It wouldn’t need to be an MU plugin specifically. It would also work as a regular plugin.

  9. prati
    prati December 4, 2015 at 7:48 am

    Hi,

    I am using gravity form on a custom post type where each post has a new author. I want to hide a certain form field for post author, or use conditional logic to show/hide for post authors.

    Thanks.

    Reply
    1. David Smith
      David Smith Staff December 4, 2015 at 8:00 am

      I’m not sure I understand. When you say post author do you mean if the current user has previously written a post, you want to show different fields? Also, am I right in assuming the user would be logged in if they had created a previous post?

    2. prati
      prati December 4, 2015 at 8:06 am

      Hi David,

      Yes, you are correct in assuming that the user is logged in, and has also written the post where the form is displayed.

    3. David Smith
      David Smith Staff December 4, 2015 at 8:32 am

      So are they editing the post via the Gravity Form? More directly, what is the purpose of the form that is being shown on the post they authored?

    4. prati
      prati December 4, 2015 at 8:51 am

      No, the form is not to edit the post. It is to contact author. I want to hide most fields of this form for the author, and instead show an HTML field to display a custom message only for the post author.

    5. David Smith
      David Smith Staff December 4, 2015 at 11:25 am

      Hi Prati, here is a snippet that that will let you populate a 1 or 0 into a field depending on whether the user is the current post’s author. You can follow this tutorial for basing conditional logic on the populated field.

    6. prati
      prati December 4, 2015 at 9:39 pm

      Hi David, I am unable to see the snippet here or a link to it. I can see the tutorial link which is pretty straightforward to implement, but will only work to show/hide fields based on user login status, not on the post author status.

      Am I missing something??

    7. prati
      prati December 5, 2015 at 1:11 am

      Thanks for this David.

      However, I am unable to make it work. Here’s what I have done,

      • Pasted your snippet to my theme’s functions.php file

      • Created a new form field with default value {user:user_login}

      Here’s what I was expecting, for post author this field will not return any value, while for other logged in users it will return their login name.

      But it is returning respective values in either case.

      I also tested with other default values like {user:ID}, {embed_post:post_author} but could not get it to work.

      Is there something I should know?

    8. David Smith
      David Smith Staff December 5, 2015 at 8:31 am

      Hi Prati, here’s a step-by-step:

      1. Install the snippet.
      2. Add a new form field (Single Line Text or Hidden).
      3. Go to the Advanced tab on the field settings.
      4. Check the “Allow field to be populated dynamically” setting.
      5. Add “is_post_author” as the parameter name.

      Now, you can add conditional logic rules to other fields based on this field. It will be “1” if they are the post author and “0” if they are not.

    9. prati
      prati December 5, 2015 at 8:47 am

      Hi David,

      Followed your step by step, and it works great.

      Can’t thank you enough for your help. You are my new hero David!

      Sincere Thanks, Prati

  10. Hugh Hou
    Hugh Hou February 22, 2015 at 2:04 pm

    This snippet is not working when enabled W3TC Monitoring using New Relic. The user_role in dynamically popular filed will just return an empty value…What is wrong? Why W3TC monitoring has anything to do with the user role ?

    Reply
    1. David Smith
      David Smith Staff February 22, 2015 at 5:43 pm

      Are you caching pages for logged in users? If so, that is likely the issue. The logged in users are seeing a logged out users cached paged.

    2. Hugh Hou
      Hugh Hou February 22, 2015 at 5:47 pm

      Actually no. Caching page for login user is turned off. And I also excluded this page from Page Cache. As a matter of fact, I turn page cache off completely and test. The page cache has nothing to do with the error – If New Relic Monitoring turn off and page cache on, everything work fine. It is when New Relic Monitoring turn on this error appear (no matter what setting page caches or browser caches are). Just FYI, those and CDN are the 4 setting I turn on at W3TC. No database caching or object caching.

  11. Tell Stevens
    Tell Stevens January 29, 2014 at 11:20 am

    When I enter this in my functions file it all works fine but I’m trying to change it to display the first name and eventually some other fields. I’m not very accomplished in wordpress but I’ve tried to piece together what I can read on the codex and stackoverflow but every time I try to change it my site breaks and the screen goes white.

    This is what I was trying. Can you point me in the right direction?

    add_filter('gform_field_value_user_first_name', 'gform_populate_user_first_name'); function gform_populate_user_info($value){ $user_info = get_userdata($userid); $first_name = $user_info->first_name; return reset($first_name); }

    Reply
    1. David Smith
      David Smith Staff October 31, 2013 at 7:58 am

      Hi Tracey, it certainly is possible. How it would work depends on where the groups data is stored for each user. Assuming it is stored in the post meta, it is likely as simple as populating the fields default admin with {user:group}. You’ll need to find out what the meta key of the “groups” data is though.

  12. Kevin Hughes
    Kevin Hughes August 9, 2013 at 12:14 pm

    Thanks for this snippet, it’s got me on the right track for what I’m trying to accomplish.. do you have any tips on modifying this to populate with a custom user registration field?

    IE, a sales rep fills out a form that is automatically populated with their ID number they entered at registration.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Dynamically Populating User Role

This field is for validation purposes and should be left unchanged.