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*/
}