Home / Archive / MemberPress: Whitelist Domains
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Whitelist Domains

This code snippet works when content is protected by MemberPress Rules. By default, logged-out visitors, and members without a proper subscription, will not be able to access protected content.

When this code snippet is active, it will whitelist HTTP referrer domains. This means, that any visitor referred from these referrer domains (e.g. affiliate website domain) will bypass MemberPress rules and paywall settings. As a result, they will gain access to the protected content.

The code must be modified by adding the domains that should allow the bypass on the following line:

$whitelist = array( "domain.com, domain1.com" );

The dummy domains (domain.com and domain1.com) should be replaced by the actual domains of your referrer websites. You can add one or more domains, separating each domain with a comma.

Note: This code snippet works only if visitors are referred from the domain specified in the code. It does not apply to visitors referred from any emails.

Code Preview
php
<?php
function mepr_custom_is_domain_whitelisted() {
    $whitelist = array(  "domain.com, domain1.com"  ); //comma-separated list of domains
    $domain = parse_url( $_SERVER[ 'HTTP_REFERER' ], PHP_URL_HOST );
    if ( !empty($domain ) && in_array( $domain, $whitelist ) ) {
        return true; //Exclude from paywall
    }
    return false;
}
function mepr_custom_exclude_from_paywall( $excluded, $post ) {
    if ( isset($_SERVER[ 'HTTP_REFERER' ]) && mepr_custom_is_domain_whitelisted() ) {
        return true; //Exclude from paywall
    }
    return $excluded;
}
add_filter( 'mepr-paywall-is-excluded', 'mepr_custom_exclude_from_paywall', 9999, 2 ); // last say
function mepr_custom_pre_run_rule_redirection( $protect, $uri, $delim ) {
    if ( isset( $_SERVER[ 'HTTP_REFERER' ] ) && mepr_custom_is_domain_whitelisted() ) {
        return false; //Do not protect this page
    }
    return $protect;
}
add_filter( 'mepr-pre-run-rule-redirection', 'mepr_custom_pre_run_rule_redirection', 9999, 3 ); // last say
function mepr_custom_pre_run_rule_content( $protect, $current_post, $uri ) {
    if ( isset( $_SERVER[ 'HTTP_REFERER' ] ) && mepr_custom_is_domain_whitelisted() ) {
        return false; //Do not protect this page
    }
    return $protect;
}
add_filter( 'mepr-pre-run-rule-content', 'mepr_custom_pre_run_rule_content', 9999, 3 ); // last say

Comments

Add a Comment