Skip to:

How to access Group Name and Group Admin Email from add_filter

  • Profile photo of twobyte


    I have created a BP_Group_Extension for a contact form that essentially hooks up the awesome but free Contact-Form-7 plugin and puts a contact form under its own tab on the Group home page.

    I want to filter the Contact Form’s php mail() function to change the message recipient to the Group Admins for that particular page. Trouble is I cannot seem to capture these group details from within the add_filter function. Here is my file Contact_Secretary_Extension.php:

     * The class_exists() check is recommended, to prevent problems during upgrade
     * or when the Groups component is disabled
    if ( class_exists( 'BP_Group_Extension' ) && class_exists( 'WPCF7') ) :
    // requires both buddypress group extension and Contact form 7 plugin
    class Contact_Secretary_Extension extends BP_Group_Extension {
         * Here you can see more customization of the config options
        private $cf7formID = "428"; // form id
        function __construct() {
            $args = array(
                'slug' => 'contact',
                'name' => 'Contact',
                'nav_item_position' => 99,
                'show_tab' => 'anyone',
                'access' => 'anyone'
            parent::init( $args );
        function display( $group_id = NULL ) {
            // we are going to get group admin details and hook into contact form 7
            echo '<div class="panel-body">';
            echo '<h3>'.__('Use this form to send the','ccc').' '.bp_get_group_name().' '.__('group secretary a message','ccc').'</h3>';
            echo do_shortcode( '[contact-form-7 id="'.$this->cf7formID.'" title="Contact Group Secretary"]' );
            echo '</div>';
    bp_register_group_extension( 'Contact_Secretary_Extension' );
    add_filter( 'wpcf7_mail_components', 'ccc_group_mail_components', 10, 2 );
    function ccc_group_mail_components( $components, $contactform ) {
    	// literally ripped straight from bp_group_list_admins() but with different output
    	global $groups_template;
    	if ( empty( $group ) ) {
    		$group =& $groups_template->group;
    	// fetch group admins if 'populate_extras' flag is false
    	if ( empty( $group->args['populate_extras'] ) ) {
    		$query = new BP_Group_Member_Query( array(
    			'group_id'   => $group->id,
    			'group_role' => 'admin',
    			'type'       => 'first_joined',
    		) );
    		if ( ! empty( $query->results ) ) {
    			$group->admins = $query->results;
    	if ( ! empty( $group->admins ) ) {
    		$recipients = array();
    		foreach( (array) $group->admins as $admin ) {
    			$recipients[] = $admin->user_email;
    		$components['recipient'] = implode(",", $recipients);
    	} else {
    		// append a little alert to subject line so we know admin emails weren‘t found.
    		$components['subject'] = $components['subject']. ' (No group admin found)' ;
    	// lets tag subject line with group section as well for traceability
    	$components['subject'] = '['.bp_get_group_name().']'.$components['subject'];
    	return $components;

    I know the script is running because test emails are getting the (No group admin found) string appended to the subject line, unfortunately it is not going to the correct email address and bp_get_group_name() is returning empty string.

    Any tips greatly appreciated thanks.


Viewing 2 replies - 1 through 2 (of 2 total)
  • Profile photo of shanebp


    After you submit the form, are you still on the group page?

    I doubt that your filter function is aware of the Group.

    Can you pass parameters to the shortcode?
    If so, pass in the $group_id$group_id = bp_get_group_id();
    Then use it in the filter to get the user ids of group admins
    groups_get_group_admins( $group_id );
    And use the result to get email addys.

    Profile photo of twobyte


    Great thanks.

    I added a custom shortcode to the form to embed group_id as a hidden input field then changed the wpcf7_mail_components filter:

    add_filter( 'wpcf7_mail_components', 'ccc_group_mail_components', 10, 3 );
    function ccc_group_mail_components( $components ) {
    	global $bp;
    	if ( isset( $_POST['group_id']) && $_POST['group_id'] > 0 ) {
    		$group_id = $_POST['group_id'];
    		$groupadmins = groups_get_group_admins( $group_id );
    		$recipients = array();
    		foreach( $groupadmins as $admin ) {
    	   		$admin_info = get_userdata($admin->user_id);
    			$recipients[] = $admin_info->user_email;
    			$components['recipient'] = implode(",", $recipients);
    		}else {
    			// append a little alert to subject line so we know admin emails weren‘t found.
    			$components['subject'] = $components['subject']. ' (Error: No group admins found)' ;
    		// lets tag subject line with group section as well for traceability
    		$group = groups_get_group( array( 'group_id' => $group_id) );
    		$components['subject'] = '['.$group->name.'] '.$components['subject'];
    	return $components;

    It works now but I was having a proper time trying to get group name using bp_get_group_name($group_id) instead of groups_get_group( array( ‘group_id’ => $group_id) ). I suppose the former must be used in the loop!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar