Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'jnews_single_post_begin', 'update_username' );
- function update_username() {
- global $wpdb;
- $query = "SELECT * FROM {$wpdb->prefix}users WHERE user_login = user_email ";
- $userlist = $wpdb->get_results( $query );
- foreach ( $userlist as $user ) {
- $username = jnews_generate_username( $user->user_email );
- global $wpdb;
- $wpdb->update(
- $wpdb->users,
- array(
- 'user_login' => $username,
- ),
- array( 'ID' => $user->ID )
- );
- }
- }
- function jnews_generate_username( $email ) {
- $username = explode( '@', $email );
- $username = $username[0];
- $username = preg_replace( '/[^A-Za-z0-9\-]/', '', $username );
- $username = $username . wp_rand( 10000, 99999 );
- if ( username_exists( $username ) ) {
- jnews_generate_username( substr( $username, 0, -5 ) );
- }
- return $username;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement