Add custom capabilities using gravity forms

Hello there. I am using Gravity forms and I am trying to add a custom capability to a user whenever the form is submitted. I’ve manage to achieve this with a Code snippet, added to the function.php, which works really well for a single form.

add_action("gform_user_registered", "add_custom_user_meta", 10, 3);
function add_custom_user_meta($user_id, $config, $entry) {

if($entry['form_id'] != 6)
    return;

wp_set_current_user( $user_id );
$user = wp_get_current_user();
$user->add_cap ("access_s2member_ccap_test");
}

Now the problem I am having is making this code work for more than one form, and more than one custom cap. The code uses !=6 where 6 is the form ID, which means any form other than number 6 will be returned.I tried to change the code, but whatever I do it just does not work. I’m sure i am missing something,

Here is my attempt at a rewrite.

add_action("gform_user_registered", "add_custom_user_meta", 10, 3);
function add_custom_user_meta($user_id, $config, $entry)

{
if($entry['form_id'] == 1)  
{return wp_set_current_user( $user_id );
$user = wp_get_current_user();
$user->add_cap ("access_s2member_ccap_test1");}

else if($entry['form_id'] == 2)
{wp_set_current_user( $user_id );
$user = wp_get_current_user();
$user->add_cap ("access_s2member_ccap_test2");}

else

{return;}
}

if anyone know how I might get it to work on more than 1 form for more than 1 ccap then I’m all ears.

Thanks for your help

1 Like

We use Gravity Forms for all of our onboarding, including S2 membership and upgrades. Pat Dumond, who might still be participating on this forum, did the initial development of 2 mu-plugins to accomplish this task. The original code used the form field numbers, but I had it modified to use the form field names, as this is more flexible in the event that new forms have a different order of fields. It is not a perfect system - it doesn’t account for all possibilities, but it has allowed us to dispense with Pro-Forms. This is not to criticize S2, but we would not expect them to provide as comprehensive a forms system as a dedicated product like GF.

Our onboarding forms use the GF User Registration Add-on - either to create a new user or update an existing user. The plugins watch for these to trigger and update the S2 membership DB accordingly.

I’ve attached the plugins.
sg_gf_new_s2hack.php adds the S2 data for new registrations.
sg_gf_exstng_s2hack.php updates the ccaps for existing members.

If you look at the code, you will see the comment:
// Assign values to variables based on form field values
The final value in each var assignment is the name of a GF field that the plugin looks for. For example you can see the CCAPS field. In the GForm, there is a hidden field named CCAPS that contains a comma delimited string of CCAPS to be added to the user’s account. This is how all of the fields work.

I’m not a php coder, but I’ve looked at the code enuf to understand what it does. Let me know if you have questions.

I’d recommend testing this on a dev site. I like to develop locally. FYI here is a tutorial I wrote on cloning a WP site for local development.

S2-GF_muPlugins.zip (2.7 KB)

1 Like

Hey thanks for this. Its a perfect solution for what I need. However i could not get it to work.
I’ll go through what I have tried.

1: Setup an mu-plugins folder in the WP content folder.
2: added a field to the form with a title CCAP and a value sol-free. (see pic)
3: changed the website in the file to reflect my website.
4: changed the api as per s2member guidlines (see here https://s2member.com/kb-article/pro-api-for-remote-operations/)

However, despite all this I still cannot get it to work. which is a real shame.
If you think I have missed something then let me know. But hey, thanks for the code.
Much appreciated

:slight_smile:

Do you have the GF User Registration Add-on setup on the form?
Attach one of your scripts.

Ok! I got it to work. I was using a - in the name for the custom caps and that was throwing things.

Thanks so much for your help. This was the last technical issue I needed to sort out. You are a star!

:grin:

1 Like