Skip to:
Content
Pages
Categories
Search
Top
Bottom
Codex HomeEmails → Custom Emails

Custom Emails

BuddyPress only includes emails for BuddyPress core functionality. Blog posting is not part of BuddyPress, but it could be an integral part of your community. Let’s look at an example to send an email to a blog post author when a user comments on their post.
Note: This requires the BP Email API which is only available in BP 2.5.0+.

Email Post Type

BuddyPress emails are a Custom Post Type so adding emails is similar to creating posts and pages. The function below will programmatically add a post and add the correct taxonomy. Taxonomies are used to connect the email with the correct Situation or action that will trigger the sending of the email. Hook your email creation to bp_core_install_emails – this insures that it will be created if you need to reset emails using the “Re-install emails” tool in the admin tools. You can call the function directly as part of activating your plugin via register_activation_hook.

function codex15766_custom_email_message() {

	// Do not create if it already exists and is not in the trash
	$post_exists = post_exists( '[{{{site.name}}}] New post comment.' );

	if ( $post_exists != 0 && get_post_status( $post_exists ) == 'publish' )
	   return;
 
	// Create post object
	$my_post = array(
	  'post_title'    => __( '[{{{site.name}}}] New post comment.', 'buddypress' ),
	  'post_content'  => __( '{{commenter.name}} commented on your blog post.', 'buddypress' ),  // HTML email content.
	  'post_excerpt'  => __( '{{commenter.name}} commented on your blog post.', 'buddypress' ),  // Plain text email content.
	  'post_status'   => 'publish',
	  'post_type' => bp_get_email_post_type() // this is the post type for emails
	);

	// Insert the email post into the database
	$post_id = wp_insert_post( $my_post );

	if ( $post_id ) {
	// add our email to the taxonomy term 'post_received_comment'
        // Email is a custom post type, therefore use wp_set_object_terms

		$tt_ids = wp_set_object_terms( $post_id, 'post_received_comment', bp_get_email_tax_type() );
		foreach ( $tt_ids as $tt_id ) {
			$term = get_term_by( 'term_taxonomy_id', (int) $tt_id, bp_get_email_tax_type() );
			wp_update_term( (int) $term->term_id, bp_get_email_tax_type(), array(
				'description' => 'A member comments on a posts',
			) );
		}
	}

}
add_action( 'bp_core_install_emails', 'codex15766_custom_email_message' );

Send Email

For the next step, hook an action to wp_insert_comment to get the comment data and then send the post author an email. In this function, you create the tokens to parse out in the email before sending. The tokens array can be anything but you usually want to keep it personal to the recipient.

bp_send_email() takes three arguments:

function codex15766_comment_inserted( $comment_id, $comment_object ) {

    if ( $comment_object ) {
		// get the post data
		$post = get_post( $comment_object->comment_post_ID );
		// add tokens to parse in email
		$args = array(
			'tokens' => array(
				'site.name' => get_bloginfo( 'name' ),
				'commenter.name' => $comment_object->comment_author,
			),
		);
		// send args and user ID to receive email
		bp_send_email( 'post_received_comment', (int) $post->post_author, $args );
    }
}
add_action( 'wp_insert_comment','codex15766_comment_inserted', 99, 2 );

Skip to toolbar