admin管理员组

文章数量:1122826

I have allowed user registration on my wordpress site. I am using Buddypress and also added the User Registration plugin by WPEverest. Both allows users to register and sends them an activation email when they sign up.

I have noticed that for both of them, the email uses the username in the "to" field (and User Registration also uses the username in the email body but Buddypress uses the first name in the email body like "Hello First Name"). Because both forms end up having the username in the email's "to" field, I believe this is a wordpress thing and not plugin specific.

I would like the email to use the user's first name in the "to" field. Is there a way to force the activation emails to use the first name instead of the username in the "to" field, as well as the email body?

Thank you.

I have allowed user registration on my wordpress site. I am using Buddypress and also added the User Registration plugin by WPEverest. Both allows users to register and sends them an activation email when they sign up.

I have noticed that for both of them, the email uses the username in the "to" field (and User Registration also uses the username in the email body but Buddypress uses the first name in the email body like "Hello First Name"). Because both forms end up having the username in the email's "to" field, I believe this is a wordpress thing and not plugin specific.

I would like the email to use the user's first name in the "to" field. Is there a way to force the activation emails to use the first name instead of the username in the "to" field, as well as the email body?

Thank you.

Share Improve this question asked May 29, 2024 at 12:29 I have many questionsI have many questions 1717 bronze badges 4
  • That's a plugin specific question. Your assumption that it's WP related is likely incorrect as the "to" field in wp_mail() is the email address. There is no other "to" field that I'm aware of, so either the way you've explained what you're referring to isn't right or this is specific to the plugins you've mentioned. – butlerblog Commented May 29, 2024 at 13:27
  • The native [wp_mail][1] function allows you to filter the attributes that are send to it, including the 'to' field. However, unless you store the first name in a global variable, you cannot access that information in the filter, because it is not passed to the mail function. This means you will have to change it before wp_mail is called. Native WordPress has over a dozen functions doing this. Most likely your plugins do as well. [1]: developer.wordpress.org/reference/functions/wp_mail – cjbj Commented May 29, 2024 at 13:32
  • 1 @cjbj, you can do it with anonymous functions without an explicit global variable with the "use" directive, but to be less generic, the information is probably in the user's data or meta and you don't need to do such things, just get the user by its email address. – Mark Kaplun Commented May 29, 2024 at 14:12
  • @MarkKaplun Smart idea! – cjbj Commented May 29, 2024 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 0

Add the following code to your theme’s functions.php file if the plugin provides a filter for modifying email headers:

function custom_user_registration_email_to_field( $email ) {
    $user_id = $email->get_user_id();
    $user = get_userdata( $user_id );
    if ( ! empty( $user->first_name ) ) {
        $email->set_to( $user->first_name . ' <' . $user->user_email . '>' );
    }
    return $email;
}
add_filter( 'user_registration_email', 'custom_user_registration_email_to_field' );

本文标签: How to make registration activation email quottoquot field use registrant39s first name