• Resolved karthic24

    (@karthic24)


    I use this code recipe to block certain email addresses from registering on my site. Originally, the code allowed only specific email addresses to join a membership level, but I modified it to block certain emails instead. Everything works well, but the issue is that the settings are created for each individual membership level. To block an email from joining my site, I have to add it to every membership level separately. Is there a way to make this code recipe use global settings? I want to add the email once in a global setting and have it restrict access to all membership levels on my website. Thank you.

    /**
    * Restrict level signups by email address or username.
    *
    * title: Restrict level signups by email address or username.
    * layout: snippet
    * collection: checkout
    * category: registration-check
    *
    * This code snippet replaces the functionality from the Register Helper add on.
    * Make sure you are running PMPro 2.9+ and have the RH add on deactivated.
    *
    * You can add this recipe to your site by creating a custom plugin
    * or using the Code Snippets plugin available for free in the WordPress repository.
    * Read this companion article for step-by-step directions on either method.
    * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
    */
    //text area for emails and user names on edit level page
    function my_add_restrict_by_email_or_username_settings() {
    $level = intval( $_REQUEST['edit'] );
    $restrict_emails = pmpro_getOption( 'level_' . $level . '_restrict_emails' );
    ?>
    <h3 class="topborder">Restrict by Email</h3>
    <p>To restrict signups to specific email addresses, enter those email addresses below, one per line. If blank, signups will not be restricted.</p>
    <textarea rows="10" cols="80" name="restrict_emails" id="restrict_emails"><?php echo esc_textarea( $restrict_emails ); ?></textarea>
    <?php

    $restrict_usernames = pmpro_getOption( 'level_' . $level . '_restrict_usernames' );
    ?>
    <h3 class="topborder">Restrict by Username</h3>
    <p>To restrict signups to specific users or usernames, enter those usernames below, one per line. If blank, signups will not be restricted.</p>
    <textarea rows="10" cols="80" name="restrict_usernames" id="restrict_usernames"><?php echo esc_textarea( $restrict_usernames ); ?></textarea>
    <?php
    }
    add_action( 'pmpro_membership_level_after_other_settings', 'my_add_restrict_by_email_or_username_settings' );

    //update the emails and usernames on save
    function my_update_restrict_by_email_or_username_settings($saveid) {
    $restrict_emails = sanitize_textarea_field( $_REQUEST['restrict_emails'] );
    pmpro_setOption( 'level_' . $saveid . '_restrict_emails', $restrict_emails );

    $restrict_emails = sanitize_textarea_field( $_REQUEST['restrict_usernames'] );
    pmpro_setOption( 'level_' . $saveid . '_restrict_usernames', $restrict_emails );
    }
    add_action( 'pmpro_save_membership_level', 'my_update_restrict_by_email_or_username_settings' );

    //check emails when registering
    function my_check_if_restricted_by_email_or_username( $okay ) {
    global $current_user;

    //only check if we're okay so far and there is an email to check
    if( $okay && ( ! empty($_REQUEST['bemail']) || ! empty( $current_user->user_email ) ) ) {
    //are we restricting emails for this level
    global $pmpro_level;
    $restrict_emails = pmpro_getOption( 'level_' . $pmpro_level->id . '_restrict_emails' );
    if( ! empty( $restrict_emails ) ) {
    $restrict_emails = strtolower( str_replace( array(";", ",", " "), "\n", $restrict_emails ) );
    if( ! empty( $current_user->user_email ) ) {
    $needle = strtolower($current_user->user_email);
    } else {
    $needle = strtolower($_REQUEST['bemail']);
    }
    $haystack = explode( "\n", $restrict_emails );
    array_walk( $haystack, function( &$val ) {
    $val = trim($val);
    return $val;
    });
    if( in_array( $needle, $haystack ) ) {
    global $pmpro_msg, $pmpro_msgt;
    $pmpro_msg = "Your account is blocked due to Terms & Conditions violation";
    $pmpro_msgt = "pmpro_error";
    $okay = false;

    //no further checks here
    return $okay;
    }
    }

    //are we restricting user names for this level
    $restrict_usernames = pmpro_getOption( 'level_' . $pmpro_level->id . '_restrict_usernames' );

    if( ! empty( $restrict_usernames ) ) {
    $restrict_usernames = strtolower( str_replace( array( ";", ",", " " ), "\n", $restrict_usernames ) );
    if( ! empty( $current_user->user_login ) ) {
    $needle = strtolower($current_user->user_login);
    } else {
    $needle = strtolower($_REQUEST['username']);
    }
    $haystack = explode( "\n", $restrict_usernames );
    array_walk( $haystack, function( &$val ) {
    $val = trim($val);
    return $val;
    });
    if( in_array( $needle, $haystack ) ) {
    global $pmpro_msg, $pmpro_msgt;
    $pmpro_msg = "Your account is blocked due to Terms & Conditions violation";
    $pmpro_msgt = "pmpro_error";
    $okay = false;
    }
    }
    }

    return $okay;
    }
    add_filter( 'pmpro_registration_checks', 'my_check_if_restricted_by_email_or_username' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Jarryd Long

    (@jarryd-long)

    Hi there, thank you for reaching out to the Paid Memberships Pro team.

    Please replace the current function my_check_if_restricted_by_email_or_username with the updated function below to set the restriction globally.

    function my_check_if_restricted_by_email_or_username( $okay ) {
    global $current_user;

    //only check if we're okay so far and there is an email to check
    if( $okay && ( ! empty($_REQUEST['bemail']) || ! empty( $current_user->user_email ) ) ) {
    //are we restricting emails for this level
    global $pmpro_level;

    //Add the emails you want to restrict here
    $restrict_emails = "[email protected],[email protected]";

    if( ! empty( $restrict_emails ) ) {
    $restrict_emails = strtolower( str_replace( array(";", ",", " "), "\n", $restrict_emails ) );
    if( ! empty( $current_user->user_email ) ) {
    $needle = strtolower($current_user->user_email);
    } else {
    $needle = strtolower($_REQUEST['bemail']);
    }
    $haystack = explode( "\n", $restrict_emails );
    array_walk( $haystack, function( &$val ) {
    $val = trim($val);
    return $val;
    });
    if( in_array( $needle, $haystack ) ) {
    global $pmpro_msg, $pmpro_msgt;
    $pmpro_msg = "Your account is blocked due to Terms & Conditions violation";
    $pmpro_msgt = "pmpro_error";
    $okay = false;

    //no further checks here
    return $okay;
    }
    }

    //Restrict by username
    $restrict_usernames = "username1,username2";

    if( ! empty( $restrict_usernames ) ) {
    $restrict_usernames = strtolower( str_replace( array( ";", ",", " " ), "\n", $restrict_usernames ) );
    if( ! empty( $current_user->user_login ) ) {
    $needle = strtolower($current_user->user_login);
    } else {
    $needle = strtolower($_REQUEST['username']);
    }
    $haystack = explode( "\n", $restrict_usernames );
    array_walk( $haystack, function( &$val ) {
    $val = trim($val);
    return $val;
    });
    if( in_array( $needle, $haystack ) ) {
    global $pmpro_msg, $pmpro_msgt;
    $pmpro_msg = "Your account is blocked due to Terms & Conditions violation";
    $pmpro_msgt = "pmpro_error";
    $okay = false;
    }
    }
    }

    return $okay;
    }

    You can then add email addresses and usernames to the $restrict_emails and $restrict_usernames variables and they will restrict globally.

    You can remove the first two functions from your snippet as they will no longer be used.

    Kind Regards,
    Jarryd
    Support Manager at Paid Memberships Pro

    Plugin Support Jarryd Long

    (@jarryd-long)

    Because there have not been any recent updates to this topic, we will be changing the status to resolved. If you have any other questions regarding this issue please start a new topic for each question to ensure we can provide the best support possible.

    Kind Regards,
    Jarryd
    Support Manager at Paid Memberships Pro

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.