PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Change user role after completing form

  1. I'm working on a site where I have a form that includes a liability waiver and rules. I'm using the members plugin for role management. I'd like to change a user's role automatically once they have completed the form. Is this possible?

    Thanks!

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  2. This isn't currently a feature. But you could certainly do it using the available hooks. You could use the gform_pre_submission filter and custom code that you write that gets the user's id if they are logged in and updates their role.

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  3. interesting... if anyone attempts this I would like to know. I have the same issue wherein I have 3 user roles and they should be at 3 different WP Admin Levels (subscriber, contributor, editor).

    I will look into that hook to see if I can do anything though I am pretty dumb when it comes to coding stuff like this.

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  4. Anyway to get a small example Carl?

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  5. I don't currently have an example as this specific functionality isn't something i've seen anyone on the forum implement before. But any developer who knows how to update a WordPress role using PHP shouldn't have any problems putting together this type of customization using available hooks.

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  6. Thanks Carl, so far the Gforms plugin has saved me weeks of custom development. Great job to all who worked on this. bunch of smarty pants.

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  7. I'll see if I can have someone on our team post a quick code snippet on how to change the user's role when a form is submitted and the user is logged in.

    Posted 12 years ago on Thursday October 13, 2011 | Permalink
  8. Is there any updates to this request?

    We would also like the user role to be changed when they register with our site using GF ...

    Posted 12 years ago on Saturday November 5, 2011 | Permalink
  9. Our second inquiry as to when this feature may be added (updated) or if GF could post a code snippet.

    Posted 12 years ago on Wednesday November 16, 2011 | Permalink
  10. Hi Guys,

    Here is a snippet that will allow you to update the logged-in user's role after the form has been submitted. If the user is not logged in, nothing will happen:

    http://pastie.org/2874838

    This would go in your theme's functions.php file.

    Posted 12 years ago on Wednesday November 16, 2011 | Permalink
  11. Thanks! Will let you know if we encounter any issues!

    Posted 12 years ago on Sunday November 27, 2011 | Permalink
  12. Hi David

    I want to create a form where the user have the option to upgrade to either a Eg. Contributor or Editor Role

    So the form will have a dropdown or something to give him this option

    Any chance you could give me the code for this?

    Posted 12 years ago on Monday January 30, 2012 | Permalink
  13. Hi Jacques,

    Currently there is no simple way to accomplish conditional roles. If you are interested in coding a custom solution, I'd be happy to give you some pointers.

    Posted 12 years ago on Tuesday January 31, 2012 | Permalink
  14. Hi,
    I would like to revive this old post.
    Currently using the latest gravity forms and user registration addon, i have been able to get all of the following working:
    user registers using 'form 1' which creates user and sets user role to 'Pending'
    admin gets email, verifies user info, uses 'New User Approve plugin' to approve, user role gets set to 'Subscriber', and user gets new password and login info by email
    on user login, 'Theme My Login plugin' pushes user to 'form 2' which 'updates' user info, displays 'thank you' page that automatically logs user out

    *** HERE is where I need help!... on 'form submission' I would like the following to happen:
    user role set to 'None'

    I tried the example posted by David Smith here:
    http://pastie.org/2874838

    Placed in my functions.php and adjusted to proper form number, but it is not changing the user's role.
    Just wondering if anyone got this to work? What am I missing?
    Thnx

    Posted 11 years ago on Monday April 1, 2013 | Permalink
  15. This code will work, but you need to change the name of the hook from gform_post_submission to gform_after_submission.

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink
  16. Ok, I have tried changing that and a few other things that I suspected might be causing this not to work. I am questioning if
    A) it is properly pulling the user_id?
    B) it is supposed to be in functions.php? (maybe in form_display.php instead?)

    Here is the code with edits commented out:
    http://pastebin.com/N8rP1qsA

    Thnx

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink
  17. The code should be in your current theme's functions.php

    On line 8, add an echo statement: echo $user_id; and see if it contains the proper user ID. That will also show you that the code is being processed for sure.

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink
  18. OK, I updated the code on my pastebin to reflect what I am currently testing with.
    I have reverted back to using:
    $user_id = get_current_user_id();

    I added the echo $user_id;

    I was pushing to a display page, so I changed to a simple msg for testing.

    On complete, I only see my simple msg and no user_id

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink
  19. That would explain why it's not working. However, we need to figure out why $user_id is empty.

    Instead of using get_current_user_id() can you try this code (just as a test)?

    [php]
    global $current_user;
    get_currentuserinfo();
    $user_id = $current_user->ID;

    Are you doing this in the theme's functions.php or a plugin you created?

    Is the user already logged in when attempting to use this functionality? As David stated previously "If the user is not logged in, nothing will happen:"

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink
  20. Yes, form is requiring user to be logged in, and user is logged in.
    I am using my current theme's functions.php, not a plugin.
    I have replaced 'get_current_user_id()' with your snippet.

    Result still does not display user_id on form complete, user is still set to Subscriber.

    Thnx

    Posted 11 years ago on Tuesday April 2, 2013 | Permalink
  21. I'm still looking to resolve this issue

    Thnx

    Posted 11 years ago on Friday April 5, 2013 | Permalink
  22. Just wanted to update this post.
    We were having issues with mixing data input form with file uploads on one form, so I made another form to be loaded after the data input form was complete, that is just for photo uploads.
    When I created the User Registration connection to Update User, I realized I could choose a different Role from the dropdown that changes the Role on form complete.
    The only problem now is that I wanted to deny user the ability to log in, which this does not fix...
    I will investigate creating a new role that denies login.

    Posted 11 years ago on Friday April 19, 2013 | Permalink