繁体   English   中英

注册没有用户名但具有唯一ID的WordPress

[英]Sign up for WordPress without username but with user id Unique

我有一个简单的注册表格WordPress。 我能够取消用户名字段,然后现在发生了什么-用户的电子邮件是用户名。 我想没有用户名,只有id用户Unique。

如果不可能,则可以发送相同的电子邮件,但以后自动添加一个号码,例如:

1@gmail.com
1@gmail.com-1
1@gmail.com-2

您可以使用wp_insert_user函数创建没有电子邮件地址的用户。

$user_data = array(
  'user_login' => 'username-here',
  'user_pass'  => 'password-here',
);

$user_id = wp_insert_user( $user_data );

if ( is_wp_error( $user_id ) ) {
    // There was an error
} else {
    // Success!
}

请参阅: https//codex.wordpress.org/Function_Reference/wp_insert_user

如果您不想使用用户名,则可以使用uniqid php函数生成一个随机的用户名。

请参阅: http//php.net/manual/en/function.uniqid.php
图书馆: https//github.com/ramsey/uuid

另外,您可以使用此辅助功能。

function uuid() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),
        mt_rand(0, 0xffff),
        mt_rand(0, 0x0fff) | 0x4000,
        mt_rand(0, 0x3fff) | 0x8000,
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

请参阅: https//solvit.io/50064cf

更新-使用简易注册表格注册后的唯一用户ID

function uuid() {
  return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),
      mt_rand(0, 0xffff),
      mt_rand(0, 0x0fff) | 0x4000,
      mt_rand(0, 0x3fff) | 0x8000,
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  );
}

add_action('erf_user_created', 'generate_unique_id_after_user_created', 10, 3);

function generate_unique_id_after_user_created( $user_id, $form_id,$sub_id ) {
  if( !empty( $user_id ) ) {
    $user_data = array(
      'ID' => $user_id,
      'user_login' => uuid(),
      'user_email' => '',
    );

    $updated_user_id = wp_update_user( $user_data );

    if ( is_wp_error( $updated_user_id ) ) {
      // There was an error, probably that user doesn't exist.
    } else {
      // Success!
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM