There are a lot of different solutions to the problem of: how do I show specific content to specific people?

This run-through is a breakdown of how you can use a simple form plugin like Gravity Forms to expand your site into a member’s access site with very little extra work. There are a tons of great plugins out there like S2Member, Paid Memberships Pro, etc., that all do a great job – but sometimes it’s just too much. This example is a scaled down version of what some of these accomplish, and you can use it as a base to customize to your own needs.

To start, we need Gravity Forms. Why this one instead of just writing an HTML form? Gravity Forms provides us some basic assets that make this entire process easier. Mainly: pre-existing integration with the WordPress user database.

We’re going to create a form with a minimal number of fields: name, email, and membership type. The membership type field is the one we will be primarily concerned with. It will determine the user’s access level on the site.

From the above form, you can see three basic fields (Name, Email, and Membership Type). Membership Type gives you three choices: Level One, Level Two, and Level Three. You could have as many different options here as you want, but keep in mind each of these will have their own security layers to content.

There are two things we need to do with this form to ensure it works they way we want. One, we need to use the Gravity Forms addon “User Registration.” This will create a new user using the email and name they’ve submitted. Two, we need to assign a user meta value for each of the membership type options so the website knows what to display to who.

In the User Registration options for the form we created, you can see how we’ve set our options. User fields are populated from the form and we’ve assigned a new user meta key and given it the value of whatever is selected in the dropdown when they sign up.

We’ve given this meta key a name of “user_membership” and it’s value will be auto-populated when the user submits the form successfully.

When you create the dropdown field of membership types, you can assign a “value” to each of the choices. This will make it easier to make sure you’re getting the right meta value when writing your functions.

Now, comes the fun part.

We’ve now got a signup form for new users, and assigned them an “access level” depending on how they signed up. The only thing to do now is to use that level to display content to our users.

There are numerous options here, and I’m going to highlight two of the most common.

One, a shortcode to conditionally display information right from within the WordPress editor.

Two, a redirect based on the user’s access level to a specific page.

The first example, the shortcode, is a simple function. We’re going to create a very easy shortcode function that we can use from within the editor to show specific users content based on their access.

When we place this function into ourĀ functions.php file in ourĀ child theme, it allows us to use a new shortcode with a specific attribute.

[[access level="one"]Content only for level one users.[/access]]
[[access level="two"]Content only for level two users.[/access]]
[[access level="three"]Content only for level three users.[/access]]

This shortcode checks the level of the user against the level in the shortcode. If they have that level, it will show them the content. If they do not have the level, it will give them nothing.

This is a great way to control visibility on forward facing pages for your users when you’re working in the WordPress editor. But what if you want to do something like not allow access to entire page, or category, etc?

There is a simple solution to this as well. There are a few different ways we could go about this – so keep in mind this is only one of many different ways to approach this.

We’re going to use another function placed in the functions.php file for this. We could also make a custom page template that would handle this as well.

This function is incredibly simple and easy to break down. There are a couple of elements that you will need to update for it to be applicable to your own project.

is_page( ID )

Replace ‘ID’ with the ID of the page you are controlling the content of. If the page ID is 3456, it would look like…

is_page( 3456 )

Replace “one” with the level value you assigned and are checking for. This is the “value” you gave the entry in the Gravity Form. In this example, we had three: one, two, three.

$hasaccess != "one"

You could change the “one” to “two” or “three” depending on what you’re doing. There are endless ways to adjust and complement your function so it is appropriate for your goal.

The last piece of code to update is the redirect page. In this example, we are checking if the user is on a specific page (by checking the page ID) and if the user has the level to view it (by checking the user’s membership meta value). If they do not have access to this page, it will redirect them to the URL of your choice. In this scenario, we redirected to google.com, but you can change that to anything (like a signup page or login page).

wp_redirect( 'https://google.com'); 

And that’s it! Seems like a lot, but it really only takes about 5 minutes and you’ve got a completely functional way to conditionally display content to your users (both via shortcode and template redirects).

Start talking about it!