wpdb - How to email user after inserting the username in database in WordPress

I have following piece of code, which inserts usernames and other details of users in the database. After inserting the

I have following piece of code, which inserts usernames and other details of users in the database. After inserting the usernames I want to email them using wp_mail();. I am unable to do so. How can I do this?

$member_details->user_login = array_map( 'sanitize_text_field', $_POST['user_login'] );
$member_details->user_role = array_map( 'sanitize_text_field', $_POST['user_role'] );
$member_details->status = array_map( 'sanitize_text_field', $_POST['status'] );

$member_details_encode = wp_json_encode( $member_details );

  global $wpdb;

  $member_result = $wpdb->insert( 'wpxa_project_members',

         array( 

              'project_id'     => $_SESSION['project_id'],
              'author_id'      => $post_author,
              'member_details' => $member_details_encode

              ),

         array( 

              '%d',
              '%d',
              '%s'

      )

    );

I have following piece of code, which inserts usernames and other details of users in the database. After inserting the usernames I want to email them using wp_mail();. I am unable to do so. How can I do this?

$member_details->user_login = array_map( 'sanitize_text_field', $_POST['user_login'] );
$member_details->user_role = array_map( 'sanitize_text_field', $_POST['user_role'] );
$member_details->status = array_map( 'sanitize_text_field', $_POST['status'] );

$member_details_encode = wp_json_encode( $member_details );

  global $wpdb;

  $member_result = $wpdb->insert( 'wpxa_project_members',

         array( 

              'project_id'     => $_SESSION['project_id'],
              'author_id'      => $post_author,
              'member_details' => $member_details_encode

              ),

         array( 

              '%d',
              '%d',
              '%s'

      )

    );
Share Improve this question edited Dec 14, 2019 at 17:17 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Jul 20, 2018 at 14:07 MineshMinesh 3173 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Assuming that $member_details->user_login is already a user in the wp_users table, then you could use the following to get their email address:

$user = get_user_by( $member_details->user_login, 'login' );

From there, you have the user's email address and can use wp_mail() to email them:

$subject = "My email subject";
$message = "My message body...";
wp_mail( $user->user_email, $subject, $message );

Let us see what code you are trying. Assuming that the hook you have bound that function to is firing, you would insert this into your function, assuming you want to email the author.

wp_mail($to = $post_author, $subject = 'This is the subject', $message = 'This is the message');

https://developer.wordpress/reference/functions/wp_mail/

Edit:

$user = get_userdatabylogin($member_details->user_login);
$user_id = $user->ID;
wp_mail($user_id, 'This is the subject', 'This is the message');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744903907a4600138.html

相关推荐

  • wpdb - How to email user after inserting the username in database in WordPress

    I have following piece of code, which inserts usernames and other details of users in the database. After inserting the

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信