Advertisement
arie_cristianD

change username to random username

Nov 16th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. add_action( 'jnews_single_post_begin', 'update_username' );
  2.  
  3. function update_username() {
  4.     global $wpdb;
  5.     $query = "SELECT * FROM {$wpdb->prefix}users WHERE user_login = user_email ";
  6.  
  7.     $userlist = $wpdb->get_results( $query );
  8.     foreach ( $userlist as $user ) {
  9.         $username = jnews_generate_username( $user->user_email );
  10.         global $wpdb;
  11.         $wpdb->update(
  12.             $wpdb->users,
  13.             array(
  14.                 'user_login' => $username,
  15.             ),
  16.             array( 'ID' => $user->ID )
  17.         );
  18.     }
  19. }
  20.  
  21. function jnews_generate_username( $email ) {
  22.     $username = explode( '@', $email );
  23.     $username = $username[0];
  24.     $username = preg_replace( '/[^A-Za-z0-9\-]/', '', $username );
  25.     $username = $username . wp_rand( 10000, 99999 );
  26.     if ( username_exists( $username ) ) {
  27.         jnews_generate_username( substr( $username, 0, -5 ) );
  28.     }
  29.     return $username;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement