Add Unauthorized Redirection Exclusions
MemberPress unauthorized redirection is a great feature that will redirect your unauthorized visitors to the page you specified in MemberPress settings. You can find more details on how this feature works in the following document: Using the Unauthorized Redirect.
The unauthorized redirection will be applied to all protected content on your site, and this might not be the best solution in all cases. This document will provide you with the code snippet and an explanation of how to use it to disable unauthorized redirection on specific posts and pages.
How To Do This?
The following code snippet will allow you to disable the unauthorized redirection on specific pages and posts based on their IDs:
// Disable MemberPress unauthorized redirection on a specific page or post
add_filter('mepr-pre-run-rule-redirection', function($redirect, $url, $delim) {
global $post;
if($post->ID === 123) {
$redirect = false;
}
return $redirect;
}, 10, 3);
The example of the code above will exclude the protected page or post with the ID of 123 from the unauthorized redirection. As a result, if an unauthorized user tries to access this page or post, instead of being redirected, the user will see the unauthorized message on this protected page. The unauthorized message will follow your MemberPress settings (at Dashboard > MemberPress > Settings > Pages tab > Default Unauthorized Message) or your rule settings if enabled.
To exclude the page or post on your site, you need to replace the ID used in the example (123) with the ID of your page or post for which the redirection should be disabled. You would change the page or post ID on the following line:
if($post->ID === 123) {
Further, if you wish to disable unauthorized redirection on multiple posts and pages, you can use the following modification of the code:
// Disable MemberPress unauthorized redirection on multiple pages and/or posts
add_filter('mepr-pre-run-rule-redirection', function($redirect, $url, $delim) {
global $post;
if(in_array($post->ID, array(123, 223, 323))) {
$redirect = false;
}
return $redirect;
}, 10, 3);
Similarly to the first version of the code, you would need to replace the IDs used in the example (123, 223, 323) with the IDs of your pages and posts. You would change the page or post ID on the following line:
if(in_array($post->ID, array(123, 223, 323))) {
NOTE: You can mix the IDs of pages and posts in the array. The order is not important.
Once you updated the code with the IDs of your pages and posts, 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.
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!