$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to get user_meta value for new user regsitered?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to get user_meta value for new user regsitered?

matteradmin10PV0评论

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   = "[email protected]";

    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 ) . "\r\n\r\n</p>";
    $message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";

    $headers = 'From: ' . $title . '<' . $from . ">\r\nReply-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 );

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   = "[email protected]";

    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 ) . "\r\n\r\n</p>";
    $message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";

    $headers = 'From: ' . $title . '<' . $from . ">\r\nReply-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 );
Share Improve this question edited Nov 21, 2015 at 5:42 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Nov 21, 2015 at 4:51 MominMomin 231 silver badge4 bronze badges 13
  • Are you using the get_user_meta() function? Looks like the $user_id gets passed to the user_register hook – Howdy_McGee Commented Nov 21, 2015 at 5:03
  • Yes.I am using get_user_meta(). $companyCreatedUserP4 = get_user_meta($user_id, 'key', true ); – Momin Commented Nov 21, 2015 at 5:08
  • but result was empty, but it should not be...When i pass static previously created user id, then i am getting the result. – Momin Commented Nov 21, 2015 at 5:09
  • Are you 100% sure that you're using the correct key name? Did you look in the database to verify? – Howdy_McGee Commented Nov 21, 2015 at 5:10
  • 2 I've edited your question and so it's easier to understand and the code is easier to read. Please try not to put code blocks in the comments here, instead edit your question and add in that code as more detail explaining what it is and why it's important. Also, when adding code to questions there's a button which looks like this {} - highlight your code and click that button and it will put it into a nice code block as you see now. This makes it much easier for people to read and for other to help you. I think I have a solution to your problem, I'm writing up an answer now just give me a sec – Howdy_McGee Commented Nov 21, 2015 at 5:47
 |  Show 8 more comments

1 Answer 1

Reset to default 2

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   = "[email protected]";

    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 ) . "\r\n\r\n</p>";
    $message .= "<p>" . sprintf( __( 'Password: %s' ), $passwor d) . "\r\n</p>";

    $headers = 'From: ' . $title . '<' . $from . ">\r\nReply-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*/
}
Post a comment

comment list (0)

  1. No comments so far