1

my website users register with phone number OTP. That's why they have no email address in their accounts. Today I need email address in user accounts. I use this code to add email address for new user registrations.

add_action("user_register", function ($user_id) {
    $user = get_userdata($user_id);
    if (empty($user->data->user_email)) {
        $args = array(
            "ID"         => $user->id,
            "user_email" => uniqid() . "@example.com",
        );
        wp_update_user( $args );
    }
});
add_filter( 'bnfw_trigger_insert_post', '__return_true' );

But I have about 3000 users that need email address in their accounts if empty . How can I do this automatically . Can make above code to work for already registered user.

and above code generate email like this 62961a20c7f2a@exmaple.com .how can use "username" instead of uniqid()

my new code according to answer

      // The Query
$user_query = new WP_User_Query( ['number' => 3500, 'paged' => 1] );

// if users were found
if ( ! empty( $user_query->get_results() ) ) {
    // then for each user
    foreach ( $user_query->get_results() as $user ) {
        // if it already has an email, skip it
        if ( ! empty( $user->user_email ) ) {
            continue;
        }

        // otherwise, add an email here...
         $args = array(
            "ID"         => $user->id,
            "user_email" => $user->user_login . "@example.com",
        );
        wp_update_user( $args );
    }
} else {
    echo 'No users found.';
}
1
  • I'm not sure why the updated code has $user = get_user_by( 'id', $user_id );, it's already getting the $user from the user query in the loop, so that line is unnecessary. It's also unclear where this code is located and when it runs, I suspect you've confused my 2 pieces of code and combined them
    – Tom J Nowell
    May 31 at 16:19

1 Answer 1

2

Doing This For a Single User In The user_register Filter

Instead of using get_userdata in your filter, fetch the user itself as a WP_User object via get_user_by:

$user = get_user_by( 'id', $user_id );

This gives you access to all of the fields of WP_User directly, e.g.:

echo $user->user_login;

Which you can then plug into your code

Doing this for all the other users

You should use WP_User_Query to fetch an array of users that do not have emails ( assuming the email is an empty string ). This would not happen inside your filter and is a separate independent piece of code.

https://developer.wordpress.org/reference/classes/wp_user_query/

Here's an example from the docs that loops through every user with a blank email and displays their name:

// The Query
$user_query = new WP_User_Query( [] );

// if users were found
if ( ! empty( $user_query->get_results() ) ) {
    // then for each user
    foreach ( $user_query->get_results() as $user ) {
        // if it already has an email, skip it
        if ( ! empty( $user->user_email ) ) {
            continue;
        }

        // otherwise, add an email here...
    }
} else {
    echo 'No users found.';
}

You can remove the echo and replace it with your check that tests the email is empty and updates the user.

Note that since you have a lot of users, you may need to set 'number' => 200, 'paged' => 1, in the WP_User_Query arguments, and re-run the loop several times with paged set to higher numbers. This is to avoid running out of memory.

8
  • Thank you for your attention. I added my new code according to your answer. But when I run this code nothing happens May 31 at 15:37
  • I have adjusted my answer. Based on your question edit you've confused the two sections of my code and merged them to create new code that is confusing and does not do what I said. Note that none of my code is a copy paste solution, it requires that you read and understand the answer
    – Tom J Nowell
    May 31 at 16:23
  • none of my code is a copy paste solution : yes that's why I never want you to answer my questions. XD. But today you saved my day. Thank you soo much Tom. I updated working code May 31 at 16:38
  • woow this updated all emails. even not empty May 31 at 17:11
  • 1
    if you're able to update all at once then go ahead and skip pagination :) and it's easy enough mistake you're not stupid
    – Tom J Nowell
    May 31 at 18:37

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.