How to get user_meta value for new user regsitered?

We have a customized the registration process. Users register themselves. When a user registers, some meta values are updated based on the $user_id. Now we need to send an email.

We have used action hook named user_register to send the email. When a user registers, an email will be sent to that user. The problem is that the meta value is not being sent even when I have gave the hook a priority of 100.

How can I fetch the meta value of that particular user using user_register hook so i can send them in an email?

Here’s the registration code:

add_action( 'user_register', 'sendMailM', 99999, 1 );
function sendMailM( $user_id ) { 
    $title  = "Title";
    $from   = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff91908d9a8f9386bf9e9b929691d19190">[email protected]</a>";

    global $wpdb, 
        $password;

    $user       = new WP_User( $user_id );
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    $companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );

    if( ! empty( $companyCreatedUserP4 ) )
    {
        $current_companyP4  = new WP_User( $companyCreatedUserP4 );
        $companyEmailP4     = $current_companyP4->user_email;
        $messageAdmin       = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
        $messageCompanyP4   = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
    }
    else
    {
        $messageAdmin = 'New User ' . $user_login . ' is registered on your site.'; 
    }

    $message  = "<p>You are now registered . Your user name and password are included in this email. </p>";
    $message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "rnrn</p>";
    $message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "rn</p>";

    $headers = 'From: ' . $title . '<' . $from . ">rnReply-To: " . $from;
    add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
    wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers );  /*admin*/

    if( ! empty( $companyEmailP4 ) )
        wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers );      /*user*/
}

This is the code to create a new user and update the meta value:

$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Read de Codex entry for user_register action, it says:

Not all user meta data has been stored in the database when this
action is triggered.

Note that doing:

$user = wp_insert_user( $userdata );
update_user_meta( $user, 'companyId', 350 );

Follow this sequence: insert user -> run user_register action -> rung your update user meta function. So, your custom user meta is not available in user_register action.

So, instead of updating user meta data after wp_insert_user(), you could do it inside the user_register action:

add_action( 'user_register', 'sendMailM' );
function sendMailM( $user_id ) {

    // Note: $_POST data is available here,
    // just in case you need to update user meta based on form input,
    // for example, $_POST['companyId']
    update_user_meta( $user_id, 'companyId', 350 );

    $title  = "Title";
    $from   = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deb0b1acbbaeb2a79ebfbab3b7b0f0b0b1">[email protected]</a>";

    global $wpdb, 
        $password;

    $user       = new WP_User( $user_id );
    $user_login = stripslashes( $user->user_login );
    $user_email = stripslashes( $user->user_email );
    // You can now access to previously updated user meta
    // Or get the companyId directly from $_POST input if needed
    $companyCreatedUserP4 = get_user_meta( $user_id, 'companyId', true );

    if( ! empty( $companyCreatedUserP4 ) )
    {
        $current_companyP4  = new WP_User( $companyCreatedUserP4 );
        $companyEmailP4     = $current_companyP4->user_email;
        $messageAdmin       = 'New User ' . $user_login . ' is registered on your site under ' . $current_companyP4->user_firstname . ' company.';
        $messageCompanyP4   = 'A new user with a user name: ' . $user_login . ' was registered under your company.';
    }
    else
    {
        $messageAdmin = 'New User ' . $user_login . ' is registered on your site.'; 
    }

    $message  = "<p>You are now registered . Your user name and password are included in this email. </p>";
    $message .= "<p>" . sprintf( __( 'Username: %s' ), $user_login ) . "rnrn</p>";
    $message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "rn</p>";

    $headers = 'From: ' . $title . '<' . $from . ">rnReply-To: " . $from;
    add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
    wp_mail( get_option( 'admin_email' ), 'New User Registration ', $messageAdmin, $headers );  /*admin*/

    if( ! empty( $companyEmailP4 ) )
        wp_mail( $companyEmailP4, 'New User Registration ', $messageCompanyP4, $headers );      /*user*/
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x