It looks like you want to do several different things here:
- Generate a semi-random number as an identifier for users when they register
- Allow that identifier to be searchable
To the first point, you can hook into the user_register
action. Here's the documentation from wp-includes/user.php
:
1774 /**
1775 * Fires immediately after a new user is registered.
1776 *
1777 * @since 1.5.0
1778 *
1779 * @param int $user_id User ID.
1780 */
1781 do_action( 'user_register', $user_id );
So, we could then add a function like this:
3 /**
4 * Generates a random identifier for newly created users.
5 *
6 * @param int $user_id The WordPress ID of the created user.
7 * @return void
8 */
9 function wp32325ae_random_user_identifier( $user_id ) {
10 $identifier = strtoupper( uniqid( 'LEF' ) );
11
12 // If you need a certain length:
13 $identifier = substr( $identifier, 0, 10 ); // Truncate to length of 10.
14
15 // Verify this doesn't collide with another user.
16 $collisions = new \WP_User_Query( [
17 'meta_key' => 'wp_custom_user_id',
18 'meta_value' => $identifier,
19 ] );
20
21 // In the case of a collision, try again.
22 if ( count( $collisions ) ) {
23 return wp32325ae_random_user_identifier( $user_id );
24 }
25
26 $result = update_user_meta( $user_id, 'wp_custom_user_id', $identifier );
27
28 if ( false === $result ) {
29 // Something went wrong! Hopefully, your code doesn't get here.
30 wp_die( ':(' );
31 }
32 }
33
34 add_action( 'user_register', 'wp32325ae_random_user_identifier' );
Some notes:
- Line 10
- You can replace this with any method you deem fit to generate a random identifier.
- Line 13
- We truncate the ID to be 10 characters total, but you can modify that as needed.
- Line 23
will try to generate another ID in the case of a collision.
- Line 28
update_user_meta
returns an int
if the meta value is created, but will return false
if the update fails, or if the value doesn't change. In theory, that should never happen for you.
- The meta key used on lines 17
and 26
, and used below, is wp_custom_user_id
. You can use whatever you want for this field name, just make sure it is something unique to your code in order to avoid collisions with WordPress or other plugins. These values will be stored in the wp_usermeta
table.
To use this later, you can do something like this:
// Assuming you already have $user available as an instance of \WP_User
echo $user->wp_custom_user_id; // Get the ID using the magic __get from $user
echo get_user_meta( $user->ID, 'wds_custom_user_id', true ); // Get the ID the "WordPress way"
As for searching, let's take a look above at the collision check...
15 // Verify this doesn't collide with another user.
16 $collisions = new \WP_User_Query( [
17 'meta_key' => 'wp_custom_user_id',
18 'meta_value' => $identifier,
19 ] );
This same code can be used to get an array of \WP_User
objects matching the search criteria, so assuming you had the search coming in over the $_GET
parameter s
:
$search = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
$users = new \WP_User_Query( [
'meta_key' => 'wp_custom_user_id',
'meta_value' => $search,
] );
Now, $users
will contain any users who have a meta_key
of wp_custom_user_id
, and a value of $search
.