【问题标题】:Email validation with Ultimate Member Plugin使用 Ultimate Member 插件进行电子邮件验证
【发布时间】:2018-01-08 19:59:48
【问题描述】:

我需要帮助修改下面的代码。我在我的 Wordpress 网站上使用 Ultimate Member 插件作为会员资格。我只希望我组织内的那些人能够注册该站点(我的部门使用不同的域作为他们的电子邮件地址,这很令人头疼,我不想参与其中)。现在,它会自动验证来自@company1.com 的电子邮件,但我需要在该代码中添加最多 10 个电子邮件地址才能执行自动验证。基本上,任何没有列出电子邮件地址的人都会被自动拒绝加入该网站。

add_action('um_before_new_user_register', 'require_google_email_for_signup');
function require_google_email_for_signup( $args ) {
    extract($args);
    if ( !strstr( $user_email, '@company1.com' ) )
        exit( wp_redirect( add_query_arg('err', 'you_must_have_googlemail') ) );
}

【问题讨论】:

    标签: php wordpress validation email


    【解决方案1】:

    使用此代码:

    <?php
    add_action('um_before_new_user_register', 'require_google_email_for_signup');
    function require_google_email_for_signup( $args ) {
        extract($args);
    
        /* add multiple domains name here */
        $allow_domains = ['company1.com', 'company2.com', 'company3.com'];
    
        /* get domain name from user email */
        $domain_name = substr(strrchr($user_email, "@"), 1);
    
        if (!in_array($domain_name, $allow_domains)){
            exit( wp_redirect( add_query_arg('err', 'you_must_have_googlemail') ) );
        }
    }
    ?>
    

    【讨论】:

    • 我将@company1.com 电子邮件地址更改为我公司的实际电子邮件域,但该代码不起作用。我尝试的每个电子邮件域都不允许我注册。
    【解决方案2】:

    strstr() is more memory intensive compared to strpos(),所以我推荐使用后者。在处理数组时,可以使用如下迭代逻辑:

    1. 设置一个变量,比如$check,到false
    2. 在 for 循环中遍历每个允许的域
    3. 只要找到匹配项(通过使用strpos()),我们将$check 设置为true 并跳出循环。这样可以确保我们在找到匹配项时不会遍历整个数组
    4. 评估$check,并决定是否抛出错误/退出

    提示:我建议您在使用strpos(或者您可以使用stripos())时将您的用户电子邮件转换为小写,因为有些用户可能会输入大小写混合的电子邮件。

    考虑到这一点,这里有一个例子:

    <?php
    add_action('um_before_new_user_register', 'require_google_email_for_signup');
    function require_google_email_for_signup( $args ) {
    
        extract($args);
    
        // Store allowed domains in an array
        $allowed_domains = ['@company1.com', '@company2.com', '@company3.com'];
    
        // Set flag to false (fail-safe)
        $check = false;
    
        // Iterate through all allowed domains
        foreach( $allowed_domains as $domain ) {
    
            // If a match is found (remember to use lowercase emails!)
            // Update flag and break out of for loop
            if ( strpos( strtolower( $user_email ), $domain ) !== false ) {
                $check = true;
                break;
            }
        }
    
        if ( !$check )
            exit( wp_redirect( add_query_arg('err', 'you_must_have_googlemail') ) );
    }
    

    【讨论】:

    • 这个 Stack Overflow 社区太棒了!特里,您的代码运行良好!
    【解决方案3】:

    此自定义代码不再适用于终极会员的最新版本 2 更新。钩子已被移除,因此不再可能使用此代码阻止电子邮件地址。有没有人对如何让它再次工作有任何建议?这是我正在使用的代码:

    /* ULTIMATE MEMBER PLUGIN DOMAIN WHITELISTING CODE SNIPPET
    enter code here`The following code will require a domain name to be  
    whitelisted for user `enter code here`registrations.     
    It forces a user email to match one included in this code at registration.
    You can add any provider you want by copying and pasting a new line as per 
    instructions.*/
    
    add_action('um_before_new_user_register', 'force_google_email_for_avnw_signup');
        function force_google_email_for_avnw_signup( $args ) {
           extract($args);
           if ( !strstr( $user_email, '@anydomain.com' ) )
    
              exit( wp_redirect( add_query_arg('err', 'whitelisted_email_required') ) );
        }
    

    【讨论】:

      【解决方案4】:

      “um_before_new_user_register”已从 2.x 版本中删除。您可以使用以下工作代码进行完整的表单验证,而不仅仅是单个字段。

      add_action('um_submit_form_errors_hook_','um_custom_validate_form', 999, 1);
      function um_custom_validate_form( $args ) {
          global $ultimatemember;
      
          $user_email = $args['user_email'];
      
          if ( !strstr( $user_email, '@domain1.com' )){
              $ultimatemember->classes['form']->add_error( 'user_email', 'You must register with a valid email address.' );
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 2017-11-19
        • 2011-01-31
        • 2014-03-05
        • 1970-01-01
        • 2023-03-21
        • 2013-12-21
        相关资源
        最近更新 更多