2

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   = "noreply@admin.no";

    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 );
  • Are you using the get_user_meta() function? Looks like the $user_id gets passed to the user_register hook – Howdy_McGee Nov 21 '15 at 5:03
  • Yes.I am using get_user_meta(). $companyCreatedUserP4 = get_user_meta($user_id, 'key', true ); – Momin Nov 21 '15 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 Nov 21 '15 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 Nov 21 '15 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 Nov 21 '15 at 5:47
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   = "noreply@admin.no";

    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*/
}
| improve this answer | |

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.