wp register script - Create a unique username with validation on wordpress registration

I want to add a unique username on registration and hide this field on the registration form. Its working so far, but it

I want to add a unique username on registration and hide this field on the registration form. Its working so far, but its not 100% perfect, because if its 3 times the same number then it will throw a failure. Does anyone have an idea how i can do this a bit better?

UPDATE::::::

function my_add_user_frontend_submitted( $submitted ) {

    // Search for the last ID registered
    $args = array(
    'orderby'      => 'registered', // registered date
    'order'        => 'DESC', // last registered goes first
    'number'       => 1 // limit to the last one, not required
    );

    $users = get_users( $args );
    $last_user = $users[0];
    $prefix = 'u_';
    $username = '1000' + $last_user->ID + 1;

    $submitted['user_login'] = $prefix . $username;
    return $submitted;
}
add_filter( 'um_add_user_frontend_submitted', 'my_add_user_frontend_submitted', 10, 1 );

OLD VERSION::::::

function generate_unique_username( $username ) {
    $username = rand(10000,100000);

    if ( ! username_exists( $username ) ) {
        echo '<input type="text" name="user_login" id="user_login" value="u_' . $username .'" />';
    } else {
        $new_username = rand(10000,100000);
        if ( ! username_exists( $new_username ) ) {
            echo '<input type="text" name="user_login" id="user_login" value="u_' . $new_username .'" />';
        } else {
            echo '<input type="text" name="user_login" id="user_login" value="u_' . rand(10000,100000) .'" />';
        }
    }
}
add_action('um_after_register_fields', 'generate_unique_username');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744894668a4599621.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信