Skip to:
Content
Pages
Categories
Search
Top
Bottom

Customizing emails template


  • ShMk
    Participant

    @shmk

    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!

Viewing 8 replies - 1 through 8 (of 8 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @shmk

    It’s definitely possible. BuddyPress uses the wp_mail() function to send emails. By default, it uses text/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


    ShMk
    Participant

    @shmk

    Thanks @henrywright,
    I’ve added the filter just before sending the mail and I immediatly remove it on bp_core_sent_user_validation_email to avoid problem on other WordPress mail that needs text/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 );

    Henry Wright
    Moderator

    @henrywright

    Yeah – those are the hooks that’ll let you filter the recipient, subject, body – with your HTML 🙂


    ShMk
    Participant

    @shmk

    Just a note: in BuddyPress bp_core_signup_send_validation_email function wp_mail is called only with the required parameters wp_mail( $to, $subject, $message ), but the function could accept also two optional parameters wp_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.


    Henry Wright
    Moderator

    @henrywright

    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 sent

    function 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


    ShMk
    Participant

    @shmk

    Nice one! 🙂


    capeleng
    Participant

    @capeleng

    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?


    capeleng
    Participant

    @capeleng

    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-plugin

    It worked like a charm for me.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Customizing emails template’ is closed to new replies.
Skip to toolbar