Gravity Forms Conditional Logic Based on User Login

February 22nd, 2014: Gravity Forms now supports conditional logic rules comparing against empty values. Updated the article verbiage accordingly.

If you implemented this snippet prior to Gravity Forms 1.8.x, the previous method no longer works. This article has been updated to reflect the new process. Be sure to recreate your conditional logic rules based on the new instructions.

Have you ever wanted to conditionally show or hide a field depending on whether the user is logged in or not? No, never? How about if you only want to require a captcha field for non-logged-in users? Or how about if you’re running a listings site (powered by Gravity Forms, of course). Anyone can submit a listing but you want to limit the ability to upload an image with the listing to registered users. Didn’t think about that, huh!?

Now that we’re agreed that this could, indeed, be useful, let’s dig in!

Step 1: Create a Hidden Field to Store the User Login

We don’t want to do any more work than we have to so we’re going to let Gravity Forms do most of the work for us. Let’s kick things off by creating a new Hidden field.

Nothing special here. Make sure you give your Hidden field an obvious label so you won’t forget the purpose of this field in the future. I like to append “(hidden)” to my Hidden fields so anywhere this field label is referenced I know it is a hidden field. Personal preference.

There isn’t a whole lot happening on the advanced field settings either… just a small bit of clever wizard magic. For the “Default Value” setting, we’re using a Gravity Form merge tag to populate the logged in user’s login name. The “Default Value” setting should look like this: {user:user_login}.

If the user isn’t logged in, the user login merge tag won’t populate anything and the value of this field will be empty. Now we’re ready to apply some conditional logic based on our newly created Hidden field.

Step 2: Apply Conditional Logic to Other Fields Based on the User Login Hidden Field

Open up the advanced field settings for whichever field you want to conditionally show or hide if the user is not logged in. At the very bottom of the advanced tab you’ll find Gravity Forms’ default conditional logic.

As of Gravity Forms v1.6.3, the conditionally logic has been significantly enhanced. With previous versions of the plugin you could only compare the values of any multi-choice field on the form (ie radio buttons, checkboxes and drop downs) with their respective values. With v.1.6.3 you can compare any field on the form with any value you’d like!

In our case, we want to compare the value of the user login hidden field we created in Step 1 to an empty value (see example below).

If the user is not logged in, our hidden field will be empty but if the user is logged in, it will be populated with the username. This allows us to create conditional logic based on whether the User Login Hidden field is empty or not. If it is, the user is not logged in; otherwise, the user is logged in.

You can change the comparison based on whether you want to show or hide the field. Here’s an example:

In plain English, this conditional logic would read: “Show this field if the user login hidden field is not empty (aka, the user is logged in)”. This means only logged-in users would be able to see this field.

Summary

Simple enough, eh? You can use this same concept in all sorts of other wizardly ways! Let us know if you use this setup and how it’s works for you. Comments are magical.

Comments

    1. Samuel Bassah
      Samuel Bassah Staff May 24, 2023 at 6:03 am

      Hi Pip,

      This tutorial wouldn’t work for this but it may be possible possible to do this with our GP Populate Anything Perk. You can use the Perk to populate a Hidden field with the current user’s memberpress level and use it to setup the conditional logic.

      Best,

    1. Dario Space
      Dario Space January 19, 2023 at 3:14 pm

      Hi Heather, we’ve already followed up via email. ​ ​To check if the logged-in user has role x, you can use Populate Anything, and you will populate the user role on a Hidden field which then can be used to test Conditional Logic. ​ ​Alternatively, you can use this snippet that will work too.

      Best,

  1. Moorey
    Moorey June 3, 2021 at 5:14 am

    Hi,

    Instead of logged in or not, is there a way I can do a conditional logic based on specific logged in users? I’ve tried both {user:display_name} with both dropdown and text fields.

    Essentially, I want to display a field if I selected my own name from the list or typed my own name in the field.

    Thanks!

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 3, 2021 at 9:18 am

      Hi Moorey,

      {user:display_name} would return the username of the user, so you could either use that merge tag to populate a text field and set up the conditional logic to show a field if the value in that text field matches a specific user name or you could use our GP Populate Anything Perk to populate a Dropdown field with the list of all usernames, so you can select from the list. The conditional logic for this will also be the same as the first one, checking if the selected value in the dropdown field matches a specific user.

      Best

  2. Bhavin
    Bhavin August 28, 2020 at 4:41 am

    This is not working. I have follow all the mentioned instructions. The form is totally disappeared after following the instructions. Working for anyone? Please help.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff August 28, 2020 at 9:38 am

      Hi Bhavin,

      That’s a bit strange it isn’t working for you because I am not really sure why the form should completely disappear. If you’re a Gravity Perk subscriber, you can drop us a line via our support form. That way we can dive deeper into this issue for you.

      Best,

  3. Stephanie Ferreira
    Stephanie Ferreira July 29, 2020 at 10:59 am

    I would like to show a field for administrators only. With conditional logic it doesn’t work, I tried to show the field only “if the user is an administrator” but when submitting the form, the submit button doesn’t work. Any solution for that? The people from gravify forms didn’t know how to help me

    Reply
    1. Ryan Donovan
      Ryan Donovan July 29, 2020 at 11:42 am

      Hello Stephanie, when you are submitting the form, is the admin item required or is it just a normal field? Could you let us know a little more of the intent to hide a field and possibly we can find a solution for you. 😃

    2. Stephanie Ferreira
      Stephanie Ferreira July 29, 2020 at 2:52 pm

      I have an email field that you can fill in using {user: user_email} which is hidden and appears already filled in for me in the records. But I would like this field to be visible to administrators. Not in the records, in the form itself. If I configure it to be visible if the user is an administrator, the submit button is opaque and does not allow sending.

    3. Ryan Donovan
      Ryan Donovan July 29, 2020 at 4:12 pm

      Hey Stephanie, I just tested this in my backend and cannot recreate this issue. Do you perhaps have another plugin that could be causing a conflict with the forms? Could you possibly create a new form and run a test to see if it works independent of what is currently setup on your current form? That way you can see if the merge tag is working as intended or if something else is an issue. 😃

    4. Stephanie Ferreira
      Stephanie Ferreira July 29, 2020 at 4:47 pm

      I found the conflict. It is in the GP Preview Submission perk. When the form has the pagination to have the function of viewing the submission, then this logical conditional does not work :(

  4. Kookool
    Kookool June 10, 2020 at 3:57 am

    Hi , I need to create a product feedback form for logged-In users: user select previous order from drop down orders list and then put feedback (comment text ,number rate ,…) for each products is selected from the order list separately (feedback for each product in the each order not about whole order) . appreciate for your help how can access to the list after check user is logged in.

    Reply
    1. Ryan Donovan
      Ryan Donovan June 10, 2020 at 10:24 am

      Hello, This is an interesting question. Will the user be logged in when submitting the list? Does the user need access to the full form or do they need access to just the list by itself? We have a way to view previous entries as well as a way to edit previous entries. Hopefully you find these helpful.

    2. Kookool
      Kookool June 11, 2020 at 9:30 am

      Hi Ryan, I am thankful with your guidance.

      Assume When an order submitted in WooCommerce (and after that the product is delivered), the buyer can submit feedback (as a comment or rating …) for each Item of the order in the GF . so ,customer select the order from drop-down orders list and then select one item of it from the related list and write a comment for that specific item of the order. I know how populating drop-down to orders but I don’t know how get drop-down list of items of the order and also how create simple comment text block in the form related to each item.

      Appreciate for your help.

    3. Ryan Donovan
      Ryan Donovan June 11, 2020 at 10:20 am

      Hello, Something like this may require more complex setup. The form would need to understand that the user is a buyer vs a seller. This can be accomplished with something like Gravity Forms Populate Anything, where you can populate the fields based on the user’s roles. You may need a fully built system to manage the entries. If you have a Gravity Perks licence we would be more than happy to take a look at your forms. Just drop us a line through our support link here.

  5. Bet Hannon
    Bet Hannon January 5, 2018 at 3:30 pm

    Hi David!

    When you say “With v.1.6.3 you can compare any field on the form with any value you’d like!”, do you mean that we no longer need your perk to do conditional logic for dates? That by default in GF I can now do something similar to this post with a hidden date field?

    Reply
    1. David Smith
      David Smith Staff January 6, 2018 at 2:11 pm

      No, the statement is a bit broad. It’s in reference to the fact that prior to GF 1.6.3 conditional logic was limited to only multi-choice fields. Post GF 1.6.3 it added support for most other field types but not all.

  6. waqas
    waqas February 11, 2017 at 4:18 pm

    Beautiful, you are super cool. Exactly what I needed.

    However, i just realized there is another way of it. It would require two primary fields with the same functionality: One field which does not apply the conditional, but with the Merge Tags inserted in the default value and remain hidden at all times. Second field the duplicate of the first one but with Conditional Logic applied and no default value.

    This method also works with compromising the Gravity Form’s Default functionality. Thanks for sharing that “Force Default Value” code.

    Reply
  7. waqas
    waqas February 10, 2017 at 7:32 pm

    I like the method, but I have one small problem with this. here is the scenario: I am using condition logic to hide the field as per the method you suggested, but the fields that are being hidden contain Default value set by a “merge tag” and I want that value to be stored no matter if the field is hidden.

    I’ll give an example I have an “Email Field” with a default value set by merge tag as {user:user_email} and has conditional login enabled for “Show this field if any of the following match, hidden field for user loggedin field is blank”. Now in this case I expect the email to be stored in the Entries, but it does not happen. Is there any workaround?

    Reply
  8. Lee Fuller
    Lee Fuller June 26, 2016 at 7:36 pm

    Curious if you have a solution that works as a conditional hide of a field or section based on a user’s role?

    Reply
  9. James Dunn
    James Dunn March 30, 2016 at 9:33 pm

    Thanks for this David. Works like a champ but I discovered something that may bite others. The “order” of this hidden field seems to be very important. I had it below the field I wanted to hide (because that field was already in the form) thinking it would not matter where this hidden field was on the page. Well, I discovered that I had to move it somewhere above the field I wanted to show/hide or it didn’t work properly. Interesting observation that I’ve never experienced before.

    Thanks for sharing this, yet another, fantastic piece of code to make our lives easier.

    Reply
    1. David Smith
      David Smith Staff March 31, 2016 at 8:36 am

      Thanks for sharing this issue, James. I tried to recreate it but failed to do so. The position of the hidden field didn’t have any impact on the conditional logic. There may be something else at play here.

    2. James Dunn
      James Dunn March 31, 2016 at 9:50 am

      Could be something else at play. I’ve had a couple of “strange issues” with this site that I’ve had to find work arounds. But, it’s all up and running and they are loving how I’ve set it up for them. So, all’s good.

      JDD

    1. Lukas
      Lukas October 21, 2016 at 11:54 am

      Thanks for the Tutorial. I managed to hide password field for logged in users with your help. However I wonder, if there Is a way to register users, if they don’t have a account yet, and update users, if they already have an account?

      Actually I can only choose register OR Update… However I have a payment form and want new users to register and recurring customers to simply log in to their account at the top of the form. Is this possible?

    2. David Smith
      David Smith Staff October 30, 2016 at 11:09 pm

      Hi Lukas, this isn’t possible out of the box. It is possible with a little customization and configuration. Unfortunately, it’s not a process that we can readily instruct.

  10. Esteban
    Esteban March 4, 2015 at 4:50 pm

    I have a questions is there a way to specify fields that only display for a specific user role? IE: admin or Editors? I have a form that I would like my admins to fill out but there are some fields that should be hidden to the general public and to some other user roles.

    In addition to this and as a separate question. I have a long lead generation form and I would like to know if I could shorten it by only showing say 3 out of the 20 fields I have. After the user submits the information and when I call them, I can fill out the rest of the information in the back end. Possible?

    Thanks, great article!

    Reply
    1. David Smith
      David Smith Staff March 5, 2015 at 7:45 am

      Hi Esteban,

      In this tutorial we populate the user login and base conditional logic on that. Here’s a tutorial where you can populate the user role. You can use the same concepts in this tutorial and apply conditional logic to the user role as well.

      https://gravitywiz.com/dynamically-populating-user-role/

      In regards to showing the user 3 fields and hiding the rest, just set the “Visibility” setting to “Admin Only” (it’s on the “Advanced” tab in the field settings).

    2. Esteban
      Esteban March 6, 2015 at 12:03 pm

      Hi David…

      Thanks for the tip. On my second point, what i mean is…sort of like different instances of the same form. Where in one instance of the form, all the fields are displayed, and in another instance where only 3 fields display. Is this possible?

    3. David Smith
      David Smith Staff March 6, 2015 at 2:11 pm

      Yes, it just depends on what dictates which instance of the form is shown. You populate whatever property should dictate which fields are shown and then use conditional logic to show/hide fields depending on that field.

    4. Esteban
      Esteban March 6, 2015 at 2:38 pm

      David… could I call you and see if you’d be interested in helping me with a project. I’m trying to setup gravity forms for about 5-6 types of leads and actually looking into using gravity as a bit of a Lead Management tool with some custom add-ons that I’d like to have develop. you can see my email in the reply, can you email me your contact info so we can talk? Thanks!

    1. David Smith
      David Smith Staff November 17, 2014 at 10:58 am

      Hi winnpress, I’m not sure I understand? It sounds like this article answers your question. Could you elaborate?

  11. Cerchi In Lega
    Cerchi In Lega August 29, 2014 at 5:16 pm

    Ciao volevo solo darti un rapido heads up . Il parole nel Lampione sembra essere in esecuzione fuori dallo schermo in Chrome . Non sono sicuro se questo è un format formattazione di rilascio o di qualcosa a che fare con browser internet compatibilità del browser ma ho pensato pensato che avrei invii a farvi sapere . La stile e il design sembrano grandi però! Spero che si ottiene il problema risolte presto . Ringraziamenti

    Reply
  12. Matt
    Matt May 13, 2014 at 6:26 pm

    Instructions followed to a T in 1.8.7 but field remains hidden for both logged in and !logged in users.

    Any ideas? Will probably use some js.

    Reply
    1. Matthew
      Matthew May 14, 2014 at 12:34 pm

      I did all these thing instructions to a ‘T’ in v1.8.7 with no luck. The conditional logic doesn’t seem to be accepting an empty value.

      I ended up taking another approach to this situation using just body_class() and some css.

      If you have <body > set up in your theme, your body tag will have a class of ‘logged-in’ if the user is logged in.

      In gforms add an ‘admin-only-field’ class to the fields you want to hide from users that are not logged in.

      Then in css do something like this:

      .admin-only-field{ display:none !important; } .logged-in .admin-only-field{ display:block !important; }

    2. David Smith
      David Smith Staff May 14, 2014 at 6:22 pm

      Hi Matthew, per our email thread, the User Login field cannot be set to “Admin only”. Admin only fields are not output with the rest of the form fields and conditional logic based on Admin only fields will not work. Try using “gf_hidden” in your field’s CSS Class setting to hide the field instead. :)

  13. Luis
    Luis April 15, 2014 at 2:39 pm

    Hi, do you have an idea of how can we make the conditional logic but also joining the user role?

    if user logged in is = “customer” role hide this field

    Thanks for your feedback.

    Reply
  14. AJD
    AJD February 22, 2014 at 7:58 pm

    In GF 1.8.4, I couldn’t get the adding the space trick to work . However conditional logic seems to not need the space any more, and is able to check for empty values.

    If I just leave the conditional logic (enter a value) field empty, it works fine. I can set ‘ is’ and ‘is not’, and it will respond if there is or is not something in the hidden field.

    Reply
    1. Chris Johnston
      Chris Johnston March 15, 2014 at 4:34 pm

      I had this setup using the space trick before gravity forms changed things. I noticed that the conditional logic no longer worked. Then I came back here looking to see if anything changed, and I saw the other comment about no longer needing the space. I tried just saving the form again with no space, and it still didn’t work. had to remove that line and re add it. You might mention in your article that if they use to have the space in there, they need to delete that logic line on the conditional logic and re add it, this time using no space. anyway, thanks for this awesome post!

  15. Elliot
    Elliot January 12, 2014 at 4:46 am

    Any ideas on the following setup: We want to create a user account in WordPress and have a user login. We then want to offer three gravity forms for them to fill in.

    Once they have submitted the forms we want to show a message that says ‘Form 1 Completed’ and hide the option to submit that form by that user.

    So once all three forms have been completed they will have three messages and no more options to send a form.

    If other users log into their account they will have the forms shown / hidden depending on whether they have completed them.

    Any ideas on this one? Thanks and great site.

    Reply
    1. David Smith
      David Smith Staff January 13, 2014 at 11:01 am

      Hi Elliot, the Better Limit Submission snippet can probably get you most of the way there. You can limit the number of times the form can be submitted and then set the time period to something ridiculous (like 30 years: 'time_period' => 60 * 60 * 24 * 365 * 30).

      Alternately, Dave Clements has an article that provides a different method for checking whether a user has submitted a form.

      Lastly, if you want to get your hands in the code, you can query the number of times a user has submitted a particular form like so: select count(id) from wp_rg_lead where created_by = 1 and form_id = 78 Replace the “1” with the ID of the user you want to check and “78” with the form ID you are checking.

  16. Craig
    Craig January 11, 2014 at 1:47 pm

    Since the 1.8 update my form will submit ok (with no errors), it will display the confirmation page but simply will not pass any of the submitted form field data.

    After some troubleshooting I tried entirely re-creating the form. That’s when I discovered that the “Enable Conditional Logic” option has been restricted “To use conditional logic, please create a drop down, checkbox or radio button field.”

    Woah! I’m using conditional logic on just about every field in my form (HTML, Name, etc) to determine if the user is logged in; using this method.

    Since this 1.8 Gravity Forms update: Is there a better way to accomplish a conditional form that displays different fields according to whether the user is logged in or not?

    Thanks!

    Reply
    1. David Smith
      David Smith Staff January 13, 2014 at 10:27 am

      Hi Craig, I’m running GF 1.8.1 on the demo site and this is what I see when I view the conditional logic setting: http://grab.by/tu7m The “Is User Logged In?” field is a hidden field which should allow you to enter anything for the conditional value. Did you configure this differently?

  17. sdk
    sdk January 2, 2014 at 4:57 am

    Awesome post! Wondering if there’s a way to HIDE fields from logged in users? For example, I would like to show member pricing for members who are logged in and non-member pricing for those that aren’t.

    Appreciated it!

    Reply
    1. David Smith
      David Smith Staff January 2, 2014 at 9:17 am

      You can hide the field from logged in users by changing the “[Show] this field…” setting to “[Hide] this field…”. Glad you like the post!

  18. Bruno
    Bruno October 21, 2013 at 5:47 pm

    I’m currently using this method, although the export/import breaks the logic. When i import the form from the XML file i have to go through all the fields delete and re-add the space and save the form. Any ideas how to fix this? Anyone else experiencing the same problem?

    Thanks

    Reply
  19. Jennifer Arnett - Starloft Photography
    Jennifer Arnett - Starloft Photography September 6, 2013 at 10:18 pm

    This was helpful. Thanks! Is there a way to hide an entire form from logged in users? I see the option to show only to logged in, but I only want logged out/unregistered users to see a particular form. I was able to use conditional logic with all the fields to hide them, but the submit button still shows up. Thanks!

    Reply
    1. David Smith
      David Smith Staff September 7, 2013 at 9:30 pm

      Hi Jennifer,

      You can do this by adding the form shortcode for the form you wish to display if the user is not logged in the “Required Login Message” option that is revealed when the “Required user to be logged in” option is checked: http://grab.by/q4rs

  20. Steve
    Steve August 22, 2013 at 5:47 pm

    Hi… Resending as my previous post got chopped off.

    I figured out how to use the basic conditional logic within section breaks, but I have the following issue:

    If the field “years at current address” is less than 2 then display the “previous address” fields below it. But if the field “years at current address” is greater than 2 then hide the previous address fields.

    This is working, but I do not want to clutter up the form so I want hide the previous address fields until they have entered something in the “years at current address” field.

    Please help.

    PS. None of my users will be logged into wordpress so i didnt think your other blog post would help me here.

    Thanks, Steve

    Reply
  21. Steve
    Steve August 22, 2013 at 5:44 pm

    Hi,

    I figured out how to use the basic conditional logic within section breaks, but I have the following issue:

    If the field “years at current address” is 2 then hide the previous address fields.

    This is working, but I do not want to clutter up the form so I want hide the previous address fields until they have entered something in the “years at current address” field.

    Please help.

    PS. None of my users will be logged into wordpress so i didnt think your other blog post would help me here.

    Thanks, Steve

    Reply
  22. Matthew Howell
    Matthew Howell May 25, 2013 at 11:10 am

    Hi David, great idea!! I made the hidden field and applied it to my first post field, works perfect. But it wont work on the rest of the fields within the form, do I need to duplicate the hidden field per each post field or something else? THANKS!

    Reply
  23. Qasim
    Qasim May 8, 2013 at 6:03 am

    Hi, I’m currently using Gravity Forms 1.6.7 and this doesn’t seem to be working for me. I’ve been at it numerous times following your instructions from the beginning but the conditional logic doesn’t seem to be recognising the blank space or something.

    Just wondering if you know whether this method no longer works on later versions? Thanks.

    Reply
  24. Sharon
    Sharon January 11, 2013 at 10:54 am

    Awesome – such a simple solution to a problem that has been plaguing me. I tried the empty field conditional and it didn’t work – adding a space is brilliant.

    Off to explore your website and see what other gems you have here.

    Reply
  25. Valdimarsson
    Valdimarsson December 11, 2012 at 9:21 am

    I’m sorry. The notification went straight in gmails spam folder. ;) I figured it out. Just went another way and told people to login and then redirected them directly to another form I’ve made F3 :)

    Thanks for the reply though :-D Much appreciated

    Reply
  26. Valdimarsson
    Valdimarsson November 28, 2012 at 6:22 pm

    Oh forgot to mention.

    If the user isn’t logged in he should be given the first forms in my F3 form: Username, password and email.

    This will be given to Gravity Forms User Registration

    Thanks in advance !

    Valdimarsson

    Reply
  27. Valdimarsson
    Valdimarsson November 28, 2012 at 6:17 pm

    Thanks for the solution given here. I have a rather complex setup and would really like if you could chip in ;) The setup is as follows: Dropdown; F1, F2, F3 When choosing F1 I have a numerous options which will reveal itself. The same goes with F2 and F3. Now the first two is plain and simple, but the third (F3), only logged in users should see the options. If they’re not logged in they should be given the opportunity to do so. The way I set gravityforms up is I’ve called Show | All | Dropdown is | F1 | for the forms I’ve wanted F1 “people” to see. And if I wanted everybody (F1,F2,F3) to see the forms: Show | Any | Dropdown is | F1, F2, F3 |

    Hope this makes sense :-) Do you have any clue what so ever?

    BTW if I get this working it’s the best tip ever. :)

    Reply
    1. David Smith
      David Smith Staff December 1, 2012 at 6:14 pm

      Hi Vladimarsson, any chance you can share a link where this form can be previewed? Might be easier to understand if I had a visual. :)

  28. Charles
    Charles August 13, 2012 at 4:08 pm

    Thanks so Mark for taking the time to write this, this is exactly what I wanted. I’ve been searching all over the place and found your gem of a solution.

    Reply
    1. David Smith
      David Smith Staff June 12, 2012 at 4:10 pm

      My pleasure Mark! Any ideas for clarifying the intent of the walk through? Always open to suggestion. :)

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.

Grab a bundle of free Gravity Forms plugins

Enter your email and receive our most popular free plugins and snippets, plus access to hundreds of others.

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