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.
function cac_email_activity_checkbox() { if ( !bp_is_groups_component() ) return; ?> <?php } add_action( 'bp_activity_post_form_options', 'cac_email_activity_checkbox' );
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.
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 );
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.
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( '
- ' );
}
jq("ul.activity-list").prepend(response);
jq("ul.activity-list li:first").addClass('new-update');
jq("li.new-update").hide().slideDown( 300 );
jq("li.new-update").removeClass( 'new-update' );
jq("textarea#whats-new").val('');
jq("#cac_activity_mail").removeAttr('checked');
/* Re-enable the submit button after 8 seconds. */
setTimeout( function() { button.attr("disabled", ''); }, 8000 );
}
});
return false;
});
});
<?php
}
add_action( 'bp_activity_post_form_options', 'cac_email_activity_js', 999 );
… and this is why you are awesome @boonebgorges, thanks for adding this feature and sharing the code!
Where can I find the “bp-default JS file” you mentioned!? I’m just not able to implement the third part of the code…
Thanks by the way for this very useful plugin!
Bojanski – It should be at wp-content/plugins/buddypress/bp-themes/bp-default/_inc/default.js