Conditional Gravity Forms Fields based on Restrict Content Pro Access Level

Restrict Content Pro - Access Levels

My latest website, The WP Butler (a professional WordPress management service) was built using, among other things, Gravity Forms, to collect information from users, and Restrict Content Pro, to manage the subscriptions, payments and different membership levels.

However, there came a point where I needed the two to talk to each other. What I wanted to achieve was to use conditional logic to only show certain fields in my forms based on the user’s access level.

If you’ve worked with Restrict Content Pro, you know that each plan can be set an access level (a number between 1 and 10), which can be used to limit what content a user sees based on their plan.

With The WP Butler, I have three different membership levels: Essential, Premium & Pro, each with an individual access level assigned to them and each membership level has different features.

So when I created forms, such as a support request form, I wanted a way of using the user’s access level to apply conditional logic within the form, to hide certain fields for instance.

In my support request form, there is an option to request a premium plugin to be installed on the user’s site. However, that feature is only available to users who have either the Premium or Pro plan. So, in order to make that apparent to people filling out the form, here’s what I did:

I added the following snippet to my functionality plugin:

Here’s a breakdown of what’s happening in the code: the user’s ID is sent through the rcp_get_subscription_id function to get the ID of the plan that the user is subscribed to. That value is then passed through the rcp_get_subscription_access_level function to get the corresponding access level, which is returned to the get_access_level function. The filter then parses that information to Gravity Forms, using the parameter “access_level” (as defined by the suffix of gform_field_value_).

Gravity Forms - RCP Access LevelFrom there, it’s a simple case of creating a single text field, and dynamically populating the field with the parameter access_level and then keeping it hidden, as you can see in this screenshot.

Then it’s just a matter of using that information to conditionally display fields as appropriate to your situation. For example, I hid the premium plugin request section of the form unless the user had an access level greater than 1 (note that this excludes users with an access level of 1, because the operator is not “greater than or equal to”), and also displayed a message to anyone with an access level less than 2 (i.e. an access level of 1), notifying them that their plan doesn’t cover that service and providing them a link to upgrade their plan if they wish.

Leave a Reply