This article demonstrates how to add membership requirements to any built-in or custom taxonomy. With this code in place, you can set up membership restrictions for your custom terms—the items that make up your taxonomy.
What are Taxonomies and Terms?
In a default WordPress site, you can create posts and assign them to any number of categories or tags. Both of these post fields help you organize your content and structure your data.
Categories and tags are two examples of what WordPress calls a taxonomy: “a way of grouping posts together based on a select number of relationships” (WordPress.org).
You’re probably closely familiar with categories and tags, but WordPress taxonomies extend way beyond these two built-in features. Your site can include other taxonomies that get added via plugins and your theme, or you may have created your own taxonomies via custom code.
For example, if you run a recipe website, you might have a custom taxonomy called “Dietary Requirements”. Within this taxonomy, there are terms such as “Gluten Free”, “Keto,” or “Vegan”.
With the code recipe below, you can now restrict posts by “Dietary Requirement”. In other words, you can offer content to your Keto members that is unique from the content accessed by members of your Vegan level.
Level up your content restrictions using post tags or other custom taxonomies with the code recipe below.
The Code Recipe (PHP)
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.
The main portion of this code that you must customize is the multidimensional array on lines 31 to 35.
- In the $term_levels array, add a new empty array where the ‘key’ is the unique taxonomy name for each taxonomy you need to protect.
- Within each taxonomy array, add a new empty array where the ‘key’ is the unique term name for each term within the taxonomy.
- Finally, set the value to each term in your custom code to an array of level IDs that have access to view items with this term in this taxonomy.
In the example code above, the ‘post_tag’ with ‘term’ name ‘members’ is restricted for logged-in members with level ID 1, 2, or 3.
Please Note: This code overrides any other PMPro-related access for restricted terms.
For example, if a post requires Level 1, but has a tag that restricts the post for Level 2, the code here blocks Level 1 members and only allows access to members with Level 2.
The code can be modified to stop the override, however you must adjust the logic on a case-to-case basis.
Why isn’t this Built into Paid Memberships Pro?
We often express that the best part of WordPress (and PMPro) is the platform’s deep customizability and extendability.
Your WordPress site is completely unique; There is no other WordPress site exactly like yours. The built-in taxonomies you may be using (do you even use tags?) plus the custom taxonomies your other plugins or code adds are uniquely yours.
Another WordPress site might be using taxonomies in their own way, too—or perhaps not using them at all.
Paid Memberships Pro natively supports post restrictions by category or per single post. If you need to slice and dice your content restrictions in other ways, we also built PMPro so that you can do that. However, it takes some extra code, as well as some confidence to get that code in place.
We have tons of confidence in you, though! You’re 100% capable of using this code recipe on your membership site.
Need some help? Our support team is here to assist you in getting this code (and any other recipe we offer) working for your membership site.