1

On my website I automatically generate an 8-digits random number $rndnr via wp_rand() during the registration process and save it as user meta.

How can I check if this number has already been assigned to any user?

I actually handle it with this, while $result reads the value from the database:

$result = $wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='user_reference_id' AND meta_value = '$rndnr'");
if(empty($result))
 return true;

But I don't know for sure if this code actually would recognize a duplicate?

How would you go about it?

Thank you.

6
  • 1
    What is $result? Please edit your question to show us where it's being set.
    – Pat J
    Jan 22, 2021 at 4:26
  • Hi Pat. I use an 8 digit number for this via wp_rand(). After that I retrieved the meta_key and the meta_value using $wpdb->get_var(). The result as $result then goes into the if(empty($result) statement. Jan 22, 2021 at 12:02
  • Note that your code can be used for SQL injection attacks, you need to use $wpdb->prepare, don't just put variables straight into SQL queries, it's dangerous
    – Tom J Nowell
    Jan 22, 2021 at 17:20
  • Also you shouldn't be using wp_rand to generate unique IDs, PHP already has functions and libraries for this that are more secure
    – Tom J Nowell
    Jan 22, 2021 at 17:23
  • @TomJNowell Thank you so much for your advice. I am still a rookie when it comes to WordPress and programming but I am improving. Would you kindly show me a working alternative? Jan 22, 2021 at 17:32

1 Answer 1

0

Here is an approach you could take to do this. It's problematic if you have a large number of users, but it could be adapted in-time.


do {
    // 1. generate your unique random number
    $result = rand();

    // 2. check it is really unique
    $is_unique = count( get_users( array(
            'meta_key'   => 'user_reference_id',
            'meta_value' => $result,
        ) ) ) === 0;

} while ( !$is_unique );

// 3. Store user meta $result
update_user_meta( $user_id, 'user_reference_id', $result );

Assumptions:

  • I've used rand() but frankly you should be using something like UUIDs and this way you wouldn't need a loop like this to check for whether your number is unique. But I've provided this approach as it's likely going to solve for your problem in the way you've designed it currently.
  • there is no handling here for WPMS or other such mods.

Edit:

Your original question keeps changing, so I'll leave this edit as the last change.

Following your comments, I'm providing code so that you can create a Unique ID every time using UUIDs. Instead of the loop to ensure your custom random codes are unique, UUIDs ensure codes are unique by design.

So simply:

// 1. Generate the random ID
$rndnr = wp_generate_uuid4();

// 2. Store the ID
update_user_meta( $user_id, 'user_reference_id', $rndnr );

// 3. Get the user meta key in the future, simply:
get_user_meta( $user_id, 'user_reference_id' );

Assumptions:

  • WordPress is at least version 4.7

In your original question you didn't state that you were using 8-digit codes. Not sure why you picked 8, but if you update your system to use UUIDs you solve your issues of uniqueness.

I'm also using update_user_meta and get_user_meta - it's unclear why you're using MySQL select statements when these helper functions are available.

6
  • Thanks Paul, it can quickly form a lot of users within a short time. I use an 8 digit number for this via wp_rand(). After that I retrieved the meta_key and the meta_value using $wpdb->get_var(). The result as $result then goes into the if(empty($result) statement. How can I solve the problem with assuming a large number of users? Jan 22, 2021 at 10:17
  • My code is similar to yours. Could you please explain more about UUIDs? And how can I integrate them with WP and save them as meta? I want to use this ID for example as customer number or when I want to publish the winner of a contest without having to disclose the real user ID or name of the user/customer. I really appreciate your help. Jan 22, 2021 at 15:27
  • I didn't realise you had code already for ensuring uniqueness - I thought that was the crux of the question you posed?
    – Paul G.
    Jan 22, 2021 at 17:34
  • I tried to achieve this with the if statement but your code is more secure as I used directly the way via database. I updated my main post above for more explanation. Jan 22, 2021 at 17:38
  • So is it generally better to use UUID? Also for a larger number of users? And if so, how can I achieve it? Could you kindly provide me a working example for this as well? I really appreciate it. – Btw. I saw your great Plugin and I'm really interested in it. I will do some tests and you probably will have a new customer soon :) Jan 22, 2021 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.