How to access Group Name and Group Admin Email from add_filter
-
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:
<?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; } endif;
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.
Peace.
- The topic ‘How to access Group Name and Group Admin Email from add_filter’ is closed to new replies.