简体   繁体   English

如何检查 WordPress 中是否存在 user_login 并通过附加到现有 user_login 以编程方式创建新用户

[英]How to check if existing user_login in WordPress and create new user programatically by appending to an existing user_login

In my WP v5.8.1, I am creating new user programmatically from public forms, using wp_create_user .在我的 WP v5.8.1 中,我使用wp_create_user从公共表单以编程方式创建新用户。 New user_login will be created by combining first_name and last_name将通过结合first_namelast_name创建新的 user_login

$new_username = strtolower(str_replace(' ', '', $_POST['first_name']) . '-' . str_replace(' ', '', $_POST['last_name']));

However, chances are there might be an existing user with same user_login.但是,可能存在具有相同 user_login 的现有用户。 Hence I want to append number (1,2,3,4,etc) to the new user_login with below function:因此,我想使用以下函数将数字(1、2、3、4 等)附加到新的user_login中:

if (username_exists($new_username)) {
    create_new_username();
} else {
    $new_username = $new_username;
}

function create_new_username() {
    $count = 0;
    while (username_exists($new_username)) :
        $new_username = $new_username . '-' . $count + 1;
        $count++;

        if (!username_exists($new_username)) {
            $create_username = $new_username;
        }
    endwhile;

    return $new_username;
}

echo create_username();

If user_login exists, the above function returns existing user_login user ID;如果user_login存在,上述函数返回现有的user_login用户 ID; it is not creating new user_login with appending with number.它不是创建新的user_login并附加数字。

Edit 1:编辑1:

I have modified the code as below without writing a separate function:我修改了下面的代码而没有编写单独的函数:

$new_username = strtolower(str_replace(' ', '', $_POST['first_name']) . '-' . str_replace(' ', '', $_POST['last_name']));

if (username_exists($new_username)) {
    $count = 0;
    while (username_exists($new_username)) :
        $new_username = $new_username . '-' . ($count + 1);
        $count++;

    endwhile;
}
echo $new_username;

This is returning a new user_login if user exists;如果用户存在,这将返回一个新的user_login however instead of appending a new series number from ($count + 1) , it is adding another new number as below:然而,它不是从($count + 1)附加一个新的序列号,而是添加另一个新数字,如下所示:

  • first-last倒数第一
  • first-last-1首尾1
  • first-last-1-2先后1-2
  • first-last-1-2-3首尾1-2-3

Whereas I need the usernames like this:而我需要这样的用户名:

  • first-last倒数第一
  • first-last-1首尾1
  • first-last-2倒数第二
  • first-last-3倒数第 3

Your function create_new_username() needs the variable $new_username it's not automatically passed to this function, and therefore is undefined.您的函数create_new_username()需要变量$new_username它不会自动传递给此函数,因此未定义。

function create_new_username($new_username) {
    $count = 0;
    while (username_exists($new_username)) :
        $new_username = $new_username . '-' . $count + 1;
        $count++;

        if (!username_exists($new_username)) {
            $create_username = $new_username;
        }
    endwhile;

    return $new_username;
}

Then however you're getting to your if(username_exists()) call... I'd have to assume through some other hook, you would have to pass the variable $new_username to your username_exists() function.然后你开始你的if(username_exists())调用......我必须通过其他一些钩子假设,你必须将变量$new_username给你的username_exists()函数。

if (username_exists($new_username)) {
    create_new_username($new_username);
} 

Since your else doesn't do anything, it's not necessary.因为你的else都不做,所以没有必要。 $new_username already is equal to $new_username $new_username已经等于$new_username

Answering my own question, comments from @Howard E has helped me to achieve what I am looking for:回答我自己的问题,@Howard E 的评论帮助我实现了我想要的:

Below is the new user name created from the public form submission:以下是从公共表单提交中创建的新用户名:

$new_username = strtolower(str_replace(' ', '', $_POST['first_name']) . '-' . str_replace(' ', '', $_POST['last_name']));

With below code, I am able to check if the user_login is exists, if exists appending a serial number to create new user_login :使用下面的代码,我可以检查user_login是否存在,如果存在附加序列号以创建新的user_login

    if (username_exists($new_username)) { // check if user_login exists
    while (username_exists($new_username)) :
        $count = preg_match_all("/.*?(\d+)$/", $new_username, $matches); // check if the existing user_login has serialised number at the end
        if ($count) { // if user_login has number at the end
            $new_username = rtrim($new_username, '-' . $count); // remove the number and '-' also before the number 
        } else {
            $count = 0; // else keep the count as 0 to add to the submitted user_login from the public form
        }
        $new_username = $new_username . '-' . ($count + 1); // this will create a new serialised user_login with numbering continuation from previous, else create new user_login as per form submission
        $count++;

    endwhile;
}
echo $new_username;

Now this is creating user_logins as below:现在正在创建 user_logins 如下:

  • first-last倒数第一
  • first-last-1首尾1
  • first-last-2倒数第二
  • first-last-3倒数第 3

Please comment if the code can be still be more simpler and efficient?请评论代码是否仍然可以更简单和高效?

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

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