Adding an “email to members” checkbox to the BuddyPress group activity stream

During the recent upgrade from BuddyPress 1.1.x to BuddyPress 1.2.x, and the subsequent move away from group wires to interactive group activity streams, one thing that some users on the CUNY Academic Commons missed was the “Notify members by email” checkbox of the old wire.

This morning I wrote a bit of code to add that kind of functionality to group activity streams. There are three functions, each of which goes in your plugins/bp-custom.php file.

First, adding the checkbox to the activity box. Notice that it only shows up when you’re on a group page.

[code language=’php’]

function cac_email_activity_checkbox() {
if ( !bp_is_groups_component() )
return;
?>


Second, handling the data when it gets to the server and sending the emails. Obviously, you’ll want to change the text of the email to match your own site and your own preferences. The line “remove_action( ‘bp_activity_after_save’ , ‘ass_group_notification_activity’ , 50 );” is there to prevent an email notification from being sent if you’re using the Group Activity Notification plugin, a big official release of which is coming soon 🙂

[code language=’php’]

function cac_email_activity_handler( $activity ) {
global $bp;

if ( $_POST[‘mailme’] == ‘mailme’ ) {

$subject = sprintf(‘[CUNY Academic Commons] New update in the group “%s”‘, $bp->groups->current_group->name );

$message = strip_tags($activity->action);
$message .= ‘

‘;
$message .= strip_tags($activity->content);

$message .= ‘

——-
‘;

$message .= sprintf(‘You recieved this message because you are a member of the group “%s” on the CUNY Academic Commons. Visit the group: %s’, $bp->groups->current_group->name, $bp->root_domain . ‘/’ . $bp->groups->current_group->slug . ‘/’ . $bp->groups->current_group->slug . ‘/’ );

//print_r($message);

if ( bp_group_has_members( ‘exclude_admins_mods=0&per_page=10000’ ) ) {
global $members_template;
foreach( $members_template->members as $m ) {
wp_mail( $m->user_email, $subject, $message );
}
}
}

remove_action( ‘bp_activity_after_save’ , ‘ass_group_notification_activity’ , 50 );
}
add_action( ‘bp_activity_after_save’, ‘cac_email_activity_handler’, 1 );
[/code]

Finally, you’ll need some Javascript to make the AJAX activity submission work correctly. This is really just a copy of what’s in the bp-default JS file, with a few added lines to make it work.

[code language=’php’]
function cac_email_activity_js() {
if ( !bp_is_groups_component() )
return;
?>

var jq = jQuery;
jq(document).ready( function() {
jq(“input#aw-whats-new-submit”).unbind(‘click’);
/* New posts */
jq(“input#aw-whats-new-submit”).click( function() {
var button = jq(this);
var form = button.parent().parent().parent().parent();

form.children().each( function() {
if ( jq.nodeName(this, “textarea”) || jq.nodeName(this, “input”) )
jq(this).attr( ‘disabled’, ‘disabled’ );
});

jq( ‘form#’ + form.attr(‘id’) + ‘ span.ajax-loader’ ).show();

/* Remove any errors */
jq(‘div.error’).remove();
button.attr(‘disabled’,’disabled’);

/* Default POST values */
var object = ”;
var item_id = jq(“#whats-new-post-in”).val();
var content = jq(“textarea#whats-new”).val();
var mailme = jq(“#cac_activity_mail:checked”).val();

/* Set object for non-profile posts */
if ( item_id > 0 ) {
object = jq(“#whats-new-post-object”).val();
}

jq.post( ajaxurl, {
action: ‘post_update’,
‘cookie’: encodeURIComponent(document.cookie),
‘_wpnonce_post_update’: jq(“input#_wpnonce_post_update”).val(),
‘content’: content,
‘object’: object,
‘mailme’: mailme,
‘item_id’: item_id
},
function(response)
{
jq( ‘form#’ + form.attr(‘id’) + ‘ span.ajax-loader’ ).hide();

form.children().each( function() {
if ( jq.nodeName(this, “textarea”) || jq.nodeName(this, “input”) )
jq(this).attr( ‘disabled’, ” );
});

/* Check for errors and append if found. */
if ( response[0] + response[1] == ‘-1’ ) {
form.prepend( response.substr( 2, response.length ) );
jq( ‘form#’ + form.attr(‘id’) + ‘ div.error’).hide().fadeIn( 200 );
button.attr(“disabled”, ”);
} else {
if ( 0 == jq(“ul.activity-list”).length ) {
jq(“div.error”).slideUp(100).remove();
jq(“div#message”).slideUp(100).remove();
jq(“div.activity”).append( ‘

6 thoughts on “Adding an “email to members” checkbox to the BuddyPress group activity stream

  1. Ray

    Boone, thanks for this tutorial.

    Was thinking of hacking into the post form for something else. This will come in handy! Bookmarked! 🙂

    Reply
  2. Pingback: Teleogistic / Wildcard email whitelists in WordPress and BuddyPress

  3. Pingback: Adding to email | Selima

  4. Pingback: Teleogistic / Hardening BuddyPress Group Documents

  5. Pingback: Teleogistic / Redirect BuddyPress activity reply links to forum’s “Leave a Reply”

Leave a Reply

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