How to customize the BuddyPress activation email

custom-buddypress-activation-email-thumbnail

BuddyPress the social networking plugin for WordPress sends an activation email when a new user registers. This email is very plain so why not add some spice to it by having your custom email. There are many forum threads out there for this topic but 99% of them don’t mention if their method is for single site or multi site. So I spent a lot of time trying customize this activation mailer without any success. Finally I sank into the code of BuddyPress and found the right set of hooks for the each environment – single or multi site.

WordPress Single site

These are the filters that contain the necessary data.

  • bp_core_signup_send_validation_email_subject
  • bp_core_signup_send_validation_email_message

Changing the subject is pretty simple.

add_filter( 'bp_core_signup_send_validation_email_subject', 'custom_buddypress_activation_subject', 10, 2 );

function custom_buddypress_activation_subject( $subject, $user_id ) {
	$user = get_userdata( $user_id );
	return $user->user_login . ' – Activate your ' . get_bloginfo( 'name' ) . ' account';
}

To change the activation message -

add_filter( 'bp_core_signup_send_validation_email_message', 'custom_buddypress_activation_message', 10, 3 );

function custom_buddypress_activation_message( $message, $user_id, $activate_url ) {
	$user = get_userdata( $user_id );
	return "Hi $user->user_login,
Thanks for registering! To complete the activation of your account please click the following link:
$activate_url
Thanks,
Jesin";
}

This is a plaintext email, you can also send a HTML email. To do this the content type of the email has to be changed to “text/html”

function set_bp_message_content_type() {
	return 'text/html';
}

add_filter( 'bp_core_signup_send_validation_email_message', 'custom_buddypress_activation_message', 10, 3 );

function custom_buddypress_activation_message( $message, $user_id, $activate_url ) {
	add_filter( 'wp_mail_content_type', 'set_bp_message_content_type' );
	$user = get_userdata( $user_id );
	return 'Hi <strong>' . $user->user_login . '</strong>,
<br><br>
Thanks for registering! To complete the activation of your account please <a href="' . $activate_url . '">click here</a>.
<br></br>
Thanks,
Jesin';
}

The email’s Content-Type is to be modified only for the BuddyPress activation message, so we act on the wp_mail_content_type hook only inside this function.

You can also use full fledged email templates to give a branded feel.

WordPress Multisite

These are the hooks which contain the activation email and its subject on a Multisite setup.

Registration without blog.

  • bp_core_activation_signup_user_notification_subject
  • bp_core_activation_signup_user_notification_message

Registration with blog.

  • bp_core_activation_signup_blog_notification_subject
  • bp_core_activation_signup_blog_notification_message

The arguments passed by the filters are different here.

add_filter( 'bp_core_activation_signup_user_notification_subject', 'custom_buddypress_activation_subject', 10, 5 );

function custom_buddypress_activation_subject( $subject, $user, $user_email, $key, $meta ) {
	return $user . ' – Activate your ' . get_bloginfo( 'name' ) . ' account';
}

Just choose the right pair of hooks depending on whether a blog is being created or not when a user registers.

add_filter( 'bp_core_activation_signup_user_notification_message', 'custom_buddypress_activation_message', 10, 5 );

function custom_buddypress_activation_message( $message, $user, $user_email, $key, $meta ) {
	$activate_url = bp_get_activation_page() . "?key=$key";
	$activate_url = esc_url( $activate_url );
	return "Hi $user,
Thanks for registering! To complete the activation of your account please click the following link:
$activate_url
Thanks,
Jesin";
}

Adding this code

So where do you add this code? There are three options

bp-custom.php

This file resides in wp-content/plugins/bp-custom.php, if it not there create it and paste the required code. This is the method I recommend because it works in sync with the BuddyPress plugin and is not affected by change of themes. Also disabling BuddyPress will cause this file NOT to be loaded thus reducing the number of loaded files.

Create a plugin

You can create a file inside wp-content/plugins/ with the following plugin header and place the code after it.

<?php
/*
Plugin Name: BuddyPress custom activation email
Plugin URI: http://jesin.tk/custom-buddypress-activation-email/
Description: Checks the health of your WordPress install
Author: Jesin A
Author URI: http://jesin.tk
*/

This gives the advantage of being able to disable this functionality if not needed.

Child Theme’s functions.php file

This is the least recommended location because changing the theme will result in lost of this functionality.

Enjoyed reading this article, subscribe to stay up-to-date with more such articles

Leave a Reply

Your email address will not be published. Required fields are marked *




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>