Customizing emails template
-
Hello,
is it possible to customize (make a more complex html template instead of the default textual one) the “user welcome” email using a plugin or specific hooks?Thanks!
-
Hi @shmk
It’s definitely possible. BuddyPress uses the
wp_mail()
function to send emails. By default, it usestext/plain
as the content type which doesn’t allow HTML. The good news is you can change this. For example:function set_content_type( $content_type ) { return 'text/html'; } add_filter( 'wp_mail_content_type', 'set_content_type' );
Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_content_type
Thanks @henrywright,
I’ve added the filter just before sending the mail and I immediatly remove it onbp_core_sent_user_validation_email
to avoid problem on other WordPress mail that needstext/plain
.I’ve also found in the code inside
bp_core_signup_send_validation_email( $user_id, $user_email, $key )
function these three hooks that I used to change the message as needed by my website:$to = apply_filters( 'bp_core_signup_send_validation_email_to', $user_email, $user_id ); $subject = apply_filters( 'bp_core_signup_send_validation_email_subject', $subject, $user_id ); $message = apply_filters( 'bp_core_signup_send_validation_email_message', $message, $user_id, $activate_url );
Yeah – those are the hooks that’ll let you filter the recipient, subject, body – with your HTML 🙂
Just a note: in BuddyPress
bp_core_signup_send_validation_email
functionwp_mail
is called only with the required parameterswp_mail( $to, $subject, $message )
, but the function could accept also two optional parameterswp_mail( $to, $subject, $message, $headers, $attachments )
.So if you want to edit also the headers and/or attachments you have to edit the BP core file adding the parameters and the corresponding filters.
So if you want to edit also the headers and/or attachments you have to edit the BP core file adding the parameters and the corresponding filters
There is a way around that (you don’t have to edit core files).
Step 1: Filter
bp_core_signup_send_validation_email_to
so the default activation email isn’t sentfunction filter_bp_core_signup_send_validation_email_to( $user_email, $user_id ) { $user_email = ''; return $user_email; } add_filter( 'bp_core_signup_send_validation_email_to', 'filter_bp_core_signup_send_validation_email_to', 10, 2 );
Step 2: Send your own activation email
function my_custom_activation_email( $subject, $message, $user_id, $user_email, $key ) { // Create your $headers and $attachments here // Send the email. wp_mail( $user_email, $subject, $message, $headers, $attachments ); } add_action( 'bp_core_sent_user_validation_email', 'my_custom_activation_email', 10, 5 );
Notes: I haven’t tested
Nice one! 🙂
I have used Henry’s method to customize the activation link email, but now I want to tweak the registration email. That’s the email that goes out after the user clicks on the link. How can I filter that?
Well, I kept searching and found the solution to my problem. Check out this post on StackExchange:
http://wordpress.stackexchange.com/questions/15304/how-to-change-the-default-registration-email-plugin-and-or-non-pluginIt worked like a charm for me.
- The topic ‘Customizing emails template’ is closed to new replies.