Additional menu

Get MemberPress today! Start getting paid for the content you create! Get MemberPress Now
  1. Home
  2. Knowledge Base
  3. Advanced Topics
  4. Custom Codes
  5. Rules and Redirections
  6. Add MemberPress Rule-Specific Unauthorized Redirections
  1. Home
  2. Knowledge Base
  3. General
  4. Protecting Content
  5. Add MemberPress Rule-Specific Unauthorized Redirections

Add MemberPress Rule-Specific Unauthorized Redirections

MemberPress has a built-in unauthorized redirection feature, which automatically redirects any unauthorized user to the URL you specified in MemberPress settings.

If you need to use multiple redirection URLs, you will need to use a custom code snippet, as this option is not available by default.

This document will provide you with the custom code and explain how you can use it to set different unauthorized redirection URLs for specific MemberPress rules.

How To Do This?

Adding the code snippet from this document will create exceptions to your unauthorized redirection URL settings.

Namely, every rule-based redirection set with this code snippet will create an exclusion from your global unauthorized redirection URL. This global redirection URL will be used for all content protected by other rules.

Note: The code snippet works only when the global redirection is enabled and set at Dashboard > Settings > MemberPress > Pages tab. To do this, you should enable the Redirect unauthorized visitors to a specific URL option. Also, you need to add the redirection URL to the URL to direct unauthorized visitors field.

To add a redirection URL to a rule based on the rule ID, you would need to add the following filter code:

// Add unauthorized redirection based on the MemberPress rule ID

add_filter( 'mepr-rule-redirect-unauthorized-url', function ( $redirect_url, $delim, $uri ) {
global $post;
if ( $post ) {
$rules = MeprRule::get_rules( $post );
if ( !empty( $rules ) ) {
$rule_ids = array_column($rules, 'ID');
if ( in_array( 101, $rule_ids ) ) {
$redirect_url = 'https://yourdomain.com/register/membership-1/';
} else if ( in_array( 201, $rule_ids ) ) {
$redirect_url = 'https://yourdomain.com/register/membership-2/';
}
}
}
return $redirect_url;
}, 999, 3 );

The code above is a sample that should be adjusted to your needs. Further down, we'll explain how you can modify the code.

Once modified, you can add the code snippet to your website to the functions.php file of your child theme. As an alternative, you can use the WPCode plugin. Please check the following document for step-by-step instructions on How To Add Custom Code Snippets in WPCode.

Note: This code snippet is not compatible with the Custom URI rules.

Set Redirection For Each Rule

For this code to work on your site, you need to add an IF statement from the sample code for each customer redirection you need.

To illustrate, the following IF statement will redirect any unauthorized user to the https://yourdomain.com/register/membership-1/ URL if they try to visit any content protected by the rule with the ID of 101:

if ( in_array( 101, $rule_ids ) ) {

$redirect_url = 'https://yourdomain.com/register/membership-1/';

The next IF statement in the sample code does the same for content protected by the rule with the ID of 201, by redirecting users to a different URL (https://yourdomain.com/register/membership-2/).

Accordingly, you need to copy this IF statement and adjust it for every rule for which you need non-global redirection. Within each IF statement, you need to adjust the following data:

  • Rule ID:
if ( in_array( 101, $rule_ids ) ) {
  • URL where users will be redirected to:
$redirect_url = 'https://yourdomain.com/register/membership-1/';

To find the rule IDs, please navigate to Dashboard > MemberPress > Rules. You will find the ID of each rule in the “ID” column. The redirection URL can be any URL from your site or external URL.

Note: Please keep in mind that IF statements should be listed in order of priority. The redirection that should have precedence needs to be listed first.

Help?

Is this not working how you think it should even after following the instructions in the video? Feel free to send us a
Support Ticket!

Was this article helpful?

Related Articles

computer girl

Get MemberPress today!

Start getting paid for the content you create.