Advanced Custom Fields (ACF) is a popular plugin available in the WordPress.org Plugin Repository. The plugin allows you to add structured information to your posts, pages, and more. In this guide, we show you three methods to handle Advanced Custom Fields membership restriction by member level.
This is an important step needed for sites that display ACF data in places that PMPro’s content filters do not already protect, ensuring that your exclusive content is only visible to logged-in members.
![Advanced Custom Fields Banner Image](https://www.paidmembershipspro.com/wp-content/uploads/2018/06/restrict-acf-by-member-level-featured-image-1024x576.jpg)
Why Protect the Display of ACF by Membership Level
Adding Advanced Custom Fields membership restriction is a decision you either know you need or know you don’t. Put plainly, if you or your developer heavily uses ACF to provide structured data display on the frontend of your membership site, you will probably need one of these recipes below to protect the field data for members only.
If you aren’t sure whether you need these code recipes, here are a few example scenarios where this approach should be used:
- Premium Content Site: Imagine a site that offers exclusive tutorials, videos, and resources to its premium members. Using ACF, the site owner can create custom fields for additional information, such as downloadable files, advanced tips, or bonus content.
- Online Course Platform: An online course platform may have different membership levels, each providing access to various course materials. With ACF, the platform can add structured data like course outlines, supplementary reading materials, and quiz questions.
- Membership-Based Blog: A blog with a membership model might offer exclusive articles, behind-the-scenes content, or special interviews to its subscribers. ACF allows the blog owner to add custom fields for these exclusive posts, including additional media, author notes, and interactive elements.
In each of these scenarios, restricting ACF fields based on membership level means that only premium members can access the extra value. This approach not only protects your content but also encourages more users to subscribe if you swap the field value with a CTA.
Option 1: The pmpro_hasMembershipLevel()
function.
In this method of Advanced Custom Fields membership restriction, we show you how to use the pmpro_hasMembershipLevel()
function to check the current logged in user’s membership level or levels before outputting the field. This is a straightforward way to protect the display of fields where they are output, since most sites using ACF plugin are already adding custom PHP functions to their theme’s template files.
In this method, you will wrap your entire code to display ACF fields with the pmpro_hasMembershipLevel()
function. Here’s an example of using the function in your template files where the fields are output:
You can read full documentation on the pmpro_hasMembershipLevel()
function here.
Option 2: The pmpro_has_membership_access()
function.
Similar to the option above, this method looks up the current post’s required membership levels. This method is a little more user friendly than the previous one. You won’t need to set specific level IDs in code. Instead, the function automatically inherits the membership protection of the post itself (set in the “Require Membership” metabox).
Here’s an example of Advanced Custom Fields membership restriction using the pmpro_has_membership_access()
function in your template files:
Option 3: The acf/format_value
Filter
This final recipe is a catch all single function that filters all fields on a member-protected page. The code uses the acf/format_value
(available in v5+ of ACF) to:
- Check if the user has access to the post.
- Optionally set the value of the returned field to empty if the person does not have access.
- Show the value of the field if the member does have access.
This method does not adjust the theme files or custom plugins where they are trying to output the field value. For this reason, you may get an inconsistent frontend appearance when fields are empty. You can combat this by updating where the values are output to first check that the field isn’t “empty”. Another fix is to return a fallback CTA value for the field, such as “Members-only”, that links to checkout.
Adding the Recipe to Your Website
You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.