最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Auto approve new users if their username is included in a predefined list

programmeradmin2浏览0评论

I need to have users' registration in my website as Subscribers.

Each user will provide a username. I need to have a list that contains a company's usernames and then I need to automatically approve any new user that his/her id is included in that list.

Is there an easy way to that support this procedure?

I need to have users' registration in my website as Subscribers.

Each user will provide a username. I need to have a list that contains a company's usernames and then I need to automatically approve any new user that his/her id is included in that list.

Is there an easy way to that support this procedure?

Share Improve this question edited Jun 26, 2019 at 5:47 zinon asked Jun 25, 2019 at 15:01 zinonzinon 536 bronze badges 3
  • 1 Hi Zinon! This community's purpose does not include recommending plugins or themes. It would be pretty easy to support this without a plugin, though. Maybe you could rephrase your question. – MikeNGarrett Commented Jun 25, 2019 at 15:13
  • @MikeNGarrett thank you for your feedback. I've updated my question. Can you please help me on this? – zinon Commented Jun 26, 2019 at 5:48
  • Have you considered scenarios such as a spammer registering using one of those approved company's usernames? – Sally CJ Commented Jul 1, 2019 at 4:42
Add a comment  | 

4 Answers 4

Reset to default 0 +50

You might put this code in functions.php :

$GLOBALS['allowed_users']= ['mikejordan', 'jamesbrown', ...];


add_action('user_register','my_function');
function my_function($user_id){
    // get user data
    $user_info = get_userdata($user_id);
    if ( in_array($user_info->login, $GLOBALS['allowed_users'] ) )
    {
       $res = $wpdb->query($wpdb->prepare("SELECT activation_key from {$wpdb->signups} WHERE user_login = %s)", $user_info->login));
       if (!empty($res)) {
          wpmu_activate_signup( $res->activation_key );
       }

    }
}

You can use WP-cli to solve your problem there are many tutorials are available for that. But i prefer this blog it is similar to your requirement

https://www.toptal/wordpress/automating-with-wp-cli

I have lots of learn from this blog and hopefully you can also learn by this blog.

Thanks me leter.

You may be do like this way

add_action( 'user_register', 'check_username_before_register_5565656', 10, 1 );

function check_username_before_register_5565656( $user_id ) {

        $user_info = get_userdata($user_id);

        //put a demo array here, you can pull from db
        $allowed_username = array('test1','test2','test3');

        if ( in_array($user_info->login, $allowed_username ) ) {
                //put you code here which skip the user validation process

            }

        }

}

I think there's a few options how you can do this, depending on at which point you want to do the username validation.

1) Validate username (and email) after (default) registration form has been submitted and before the user is created

Error messages are shown on the registration page.

function my_prefix_registration_errors( $errors, $sanitized_user_login, $user_email ) {
  if ( ! is_valid_username( $sanitized_user_login ) ) {
    $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Invalid username.', 'my_textdomain' ) );
  }
  if ( ! is_valid_email( $user_email ) ) {
    $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: Invalid email.', 'my_textdomain' ) );
  }
  return $error;
}
add_filter( 'registration_errors', 'my_prefix_registration_errors', 10, 3 );

2) Validate username when the user is being created with wp_insert_user()

// step 1, validate username
function my_prefix_pre_user_login( $sanitized_user_login ) {
  if ( ! is_valid_username( $sanitized_user_login ) ) {
    $sanitized_user_login = 'illegal_username';
  }
  return $sanitized_user_login;
}
add_filter( 'pre_user_login', 'my_prefix_pre_user_login' );

// step 2, add blocked usernames. If username is invalid after step 1, it will trigger "invalid_username" WP_Error with this filter thus blocking the registration
function my_prefix_illegal_user_logins() {
  return array('illegal_username');
}
add_filter( 'illegal_user_logins', 'my_prefix_illegal_user_logins' );

3) Allow all registrations, but change user role, if illegal username after new registration

function my_prefix_register_new_user( $user_id ) {
  $user = get_user_by( 'ID', $user_id );
  if ( ! is_valid_username( $user->user_login ) ) {
    $user->set_role('invalid_registration');
  }
}
add_filter( 'register_new_user', 'my_prefix_register_new_user' );

4) Building upon T.Toduas answer regardin multisite user activation.

Activation without the use of $GLOBALS.

function t_toduas_user_register_function($user_id){
  // get user data
  $user_info = get_userdata($user_id);
  if ( is_valid_username( $user_info->login ) ) {
    $res = get_activation( $user_info->login );
    if ( !empty( $res ) ) {
      wpmu_activate_signup( $res->activation_key );
    }
  }
}
add_action('user_register','t_toduas_user_register_function');

Helper functions for the examples above

function get_allowed_usernames() {
  return array(
    'donald',
    'goofy'
    'mickey'
  );
}

function get_valid_domain() {
  return 'example';
}

function is_valid_username( $username ) {
  return in_array( $username, get_allowed_usernames() );
}

// crude email validation
function is_valid_email( $email ) {
  if ( is_email( $email ) ) {
    return false;
  }
  $email_pieces = explode( '@', $email );
  return in_array( get_valid_domain(), $email_pieces );
}

function get_activation( $login ) {
  global $wpdb;
  return $wpdb->query($wpdb->prepare("SELECT activation_key from {$wpdb->signups} WHERE user_login = %s)", $login));
}
发布评论

评论列表(0)

  1. 暂无评论