I am using wp_insert_user
to insert new users and before inserting the user it checks whether the user exists or not and only the new user are registered. Now I want to notify both users and admins about the user registration.
Here is the code of wp_insert_user
:
$user = $mail['header']->fromaddress;
$email = strstr($mail['header']->fromaddress, '<',true);
for ($j=1; $j < $total; $j++) {
$mail = $emails-> get($j);
if(email_exists($email)){
add_post_meta($post_id, 'ticket_username', $user, True);
add_post_meta($post_id, 'ticket_email', $email, True);
}else{
$userdata = array(
'user_login' => ucwords(strstr($mail['header']->fromaddress, '<',true)),
'user_pass' => '',
'user_nicename' => ucwords(strstr($mail['header']->fromaddress, '<',true)),
'display_name' => ucwords(strstr($mail['header']->fromaddress, '<',true)),
'user_email' => strstr($mail['header']->fromaddress, '<'),
'role' => 'support_customer',
);
$user_id = wp_insert_user($userdata);
if ( ! is_wp_error( $user_id ) ) {
wp_update_post( array(
'ID' => $post_id,
'post_author' => $user_id,
) );
}
}
}
And here is the code for notifying users and admins:
if ( !function_exists( 'wp_new_user_notification' ) ) {
function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
$user = new WP_User($user_id);
$user_data = get_userdata( $user_id );
$firstname = $user_data->first_name;
$user_login = stripslashes( $user_data->user_login );
// URLs
$site_url = site_url();
$ads_url = site_url( 'ads/' );
$login_url = site_url();
// Email variables
$headers = 'From: EXAMPLE.INFO <[email protected]>' . "rn";
$blog_name = get_option( 'blogname' );
$admin_subject = 'New User Registration on ' . $blog_name;
$welcome_subject = 'Welcome to My site!';
$welcome_email = stripslashes( $user_data->user_email );
$admin_email = get_option('admin_email');
$admin_message =
<<<EOT
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<div class="content">
<div class="wrapper">
<p>New user registration on your blog: {$blog_name}.</p>
<p>Username: {$user_login}</p>
<p>Email: {$welcome_email}</p>
</div>
</div>
</body></html>
EOT;
$welcome_message =
<<<EOT
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<div class="content">
<div class="wrapper">
<table width="100%"><tr><td>
Hello {$firstname},<br />
To log into your account,
go <a href="{$login_url}">visit our site</a>
and use the credentials below.<br />
Your Username: {$user_login}<br />
Your Password: {$plaintext_pass}<br />
</td></tr></table>
</div>
</div>
</body></html>
EOT;
print_r($admin_email);
//die()
wp_mail( $admin_email, $admin_subject, $admin_message, $headers );
wp_mail( $welcome_email, $welcome_subject, $welcome_message, $headers );
} // End of wp_new_user_notification()
}
This code is not working I guess as it is not sending any emails. I am quite new to all this, so I am hoping to get some help here.