D
D
Dmitry Filandor2018-05-12 20:42:12
WordPress
Dmitry Filandor, 2018-05-12 20:42:12

How to send an email to a registered user (explanation)?

Hello! It is necessary to send a welcome letter to each registering user, in principle, everything is clear:
an example from offsite:

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {

    if ( isset( $_POST['first_name'] ) )
        update_user_meta($user_id, 'first_name', $_POST['first_name']);

}

I don’t understand where the value of the variable comes from in the $user_id myplugin_registration_save function?
Another example not from off site:
<?php
  function send_welcome_email_to_new_user($user_id) {
    $user = get_userdata($user_id);
    $user_email = $user->user_email;
    // for simplicity, lets assume that user has typed their first and last name when they sign up
    $user_full_name = $user->user_firstname . $user->user_lastname;
    // Now we are ready to build our welcome email
    $to = $user_email;
    $subject = "Hi " . $user_full_name . ", welcome to our site!";
    $body = '
              <h1>Dear ' . $user_full_name . ',</h1></br>
              <p>Thank you for joining our site. Your account is now active.</p>
              <p>Please go ahead and navigate around your account.</p>
              <p>Let me know if you have further questions, I am here to help.</p>
              <p>Enjoy the rest of your day!</p>
              <p>Kind Regards,</p>
              <p>poanchen</p>
    ';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    if (wp_mail($to, $subject, $body, $headers)) {
      error_log("email has been successfully sent to user whose email is " . $user_email);
    }else{
      error_log("email failed to sent to user whose email is " . $user_email);
    }
  }
  add_action('user_register', 'send_welcome_email_to_new_user');
?>

likewise, where is the initialization variable $user_id ? I code in c#, where if you pass a parameter to a function through a variable, then you need to pass this variable in the function call, for example, on a sharpe it would look like this:
add_action('user_register', 'send_welcome_email_to_new_user (111)');

или

 add_action('user_register', 'send_welcome_email_to_new_user(getCurrentUser())');

but it's not clear. Maybe $user_email is a global variable in the theme's function.php file and is available throughout the file?
PS plz throw a link to interesting material to get a little stretch in php/wordpress

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Mavlikhanov, 2018-05-13
@LifeAct

Hello. First of all, I want to note that you need to see how the function worksadd_action()
The function send_welcome_email_to_new_user()to get the user's id automatically, see user_register
And as for the interesting material, alas, I can't tell you anything, since this is not my forte. Nuachitsya can be as follows. Download any html theme. And try to create a topic. And all the functionality of the theme is implemented by plugins.
I hope I helped you :) if anything, please contact

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question