6.3 Advanced Rule Features

Created by Danny Wong, Modified on Sun, 9 Aug, 2020 at 2:46 PM by Danny Wong

Regex 

For advanced users, you can enable the regex option to protect anything that matches your expression. The only character that cannot be used in the regex box is the hash (#). All others are safe to use. So for example, to protect a directory called "mycoolfolder" you could use something like: ^/mycoolfolder/.*

Note: for the regex users, the matches happen from $_SERVER['REQUEST_URI'], which is everything after the domain. So if you're WordPress is in a sub directory (http://site.com/wordpress/) you'll need to add ^/wordpress/ to the beginning of your patterns.

Partial Content Codes 

Do you want to protect some paragraphs, images, or even hide widgets on a page from unauthorized members, but leave the rest of the page available to everyone? Well MemberPress makes that easy! Each Rule and Membership you create comes with a Shortcode and PHP Snippet which you can use to hide selected portions of your content based on that individual rule or by content associated with your Memberships.

The Shortcodes will be the most commonly used as they will work on all WordPress Pages, Posts, and Custom Post Types. Shortcodes look like [mepr-active rule="1" ifallowed="show"]Anything in-between gets protected[/mepr-active] and simply wrap around the content you want to protect from the unauthorized users. The rule="1" part is where you specify the ID of the Rule which should be applied to this protected content. The ifallowed="show" part is where you specify whether to show or hide this partial content if the user has access.

The [mepr-active] shortcode has also unauth parameter that can be used like this: [mepr-active rule="1" ifallowed="show" unauth="message"]Anything in-between gets protected[/mepr-active]. If set to unauth="message" - the user will be shown the unauthorized message when they are not authorized. If set to unauth="login" - the user will see the login form when they are unauthorized. If set to unauth="both" - the user will see the default unauthorized message followed by the login form. NOTE: when set to, "message" or, "both" AND the rule has a custom unauthorized message it will display the custom unauthorized message from the rule instead of the global one.

Using both ifallowed="show" and ifallowed="hide" on the same page is a great way to show teaser content to guests and unauthorized members, but then hide that teaser content after they have paid and logged in and instead show the full premium content.

If you want to leave a mental note for yourself on the backend of your site you can add a third part to this shortcode which looks like description="gold_membership_only". This way you can easily remember when looking over your page or post six months+ later what this shortcode was for. Be sure to only use letters, numbers, and underscores for the description. Spaces and quotes can cause unexpected results. Here's an example with a description included - [mepr-active rule="1" ifallowed="show" description="gold_membership_only"]Anything in-between gets protected[/mepr-active].

The PHP Snippet is for advanced users who maybe want to protect some partial content inside of a WordPress template, theme file, or even widgets. The PHP Snippet is a simple if statement that need to be formatted like the following examples: (Only one of the parameters below can be applied per snippet)

PHP Snippet
Description
rule(s)
If you plan to use a single Rule ID, then set this to rule="X". If you plan to use multiple Rule ID's, then set this to rules="X, XX, XXX". Where X's = the ID's of the Rule(s). Where to find a Rule's ID? An example could be <?php if(current_user_can('mepr-active','rule:1')): ?>Content to protect<?php endif; ?> or <?php if(current_user_can('mepr-active','rules:1,2,3')): ?>Content to protect<?php endif; ?> for more than one rule.
membership(s)
If you plan to use a single Membership ID, then set this to membership="X". If you plan to use multiple Membership ID's, then set this to memberships="X, XX, XXX". Where X's = the ID's of the Membership(s). Where to find a Membership's ID? Examples of this could be <?php if(current_user_can('mepr-active','membership:1')): ?>Content to protect<?php endif; ?> or <?php if(current_user_can('mepr-active','memberships:111,222,333')): ?>Content to protect<?php endif; ?>

Note: You can also find these snippets for each rule you create when clicking the button "Partial Content Codes" when creating a new rule or editing an existing one.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article