I'd like it so that when a user creates a profile, there is a custom form that states "ID Number", which is then semirandomly generated. So, for example, if Joe creates an account, he'll be asked a bunch of custom field questions and then get automatically generated a field stating:

User ID: LEF2309824 , where the first 3 characters are preset and the numbers following are within a set of parameters. This number would then be tied to the users account.

I haven't committed to a user system, and I'm not really sure where to start with this problem. The thing about this is that the randomly generated number needs to then be searchable by any user entering the site.

For example, Bill comes on and wants to check Joe's ID. He can then type LEF2309824 into a search field which then would pull up Joe's profile.

I'm currently reading about get_users, and maybe that is a possible solution to the searching, but I'm not certain about the number generation still.


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' ) );
 12     // If you need a certain length:
 13     $identifier = substr( $identifier, 0, 10 ); // Truncate to length of 10.
 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     ] );
 21     // In the case of a collision, try again.
 22     if ( count( $collisions ) ) {
 23         return wp32325ae_random_user_identifier( $user_id );
 24     }
 26     $result = update_user_meta( $user_id, 'wp_custom_user_id', $identifier );
 28     if ( false === $result ) {
 29         // Something went wrong! Hopefully, your code doesn't get here.
 30         wp_die( ':(' );
 31     }
 32 }
 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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