Add Rule-Specific Unauthorized Redirections

MemberPress comes with the built-in unauthorized redirection feature, which will automatically redirect any unauthorized user to the URL you specified in MemberPress settings. To learn more about setting up unauthorized redirection and using this feature, please check the following document: Using the Unauthorized Redirect.

In cases where you might need to use multiple redirection URLs, you would 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 (the "URL to direct unauthorized visitors to" option) set at Dashboard > Settings > MemberPress > Pages tab. This global redirection URL will be used for all content protected by other rules.

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

// Add unauthorised redirection based on the MemberPress rule ID


add_filter( 'mepr-rule-redirect-unauthorized-url', function ( $redirect_url, $delim, $uri ) {

$current_post = MeprUtils::get_current_post( );

$rules = MeprRule::get_rules( $current_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 ); //This needs to run last

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!

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.