Create user friendly gated content in WordPress with Advanced Custom Fields
A recent client of mine wanted to restrict some of his content based off of a user’s “Role”. I already had Advanced Custom Fields Pro on his website and didn’t wanted to refrain from using another plugin so I found a way to do it in ACF.
Create the Field in ACF
I have the pro version of ACF and used the repeater field but it can be done without it.
The client wanted to display book excerpts and have some of them be open to the public and others to be gated to users set to specific roles.
As you can see below I set up the field to display the information necessary and added a sub field of “Is this content gated?”.
In the sub field I set the Field Type to “Radio Button”, set some instructions for my client and added the choices: “No, Yes”. I added “No” first so it will default to No when he adds a new excerpt. See Below:
This is what it will look like when editing your page but before anything works we need to add the code to our template.
Add the code to your template.
Here is the code I used:
In short, the code checks the current user role, defines roles that are allowed to view the gated content and filters the content based off of user role. In this specific case my client decided that the roles: Teacher, Member and Administrator could see the gated content. I set up custom user roles before hand but any “role” can be used in this scenario.