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.
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.
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.
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!