Send email after rejection
-
Hi,
Is it possible that Buddypress sends an email when a friendship request has been rejected?
If yes, how to achieve this?!
-
The
friends_friendship_rejected
hook can be used here because it fires after a friendship request is rejected. Here’s an example:add_action( 'friends_friendship_rejected', function( $friendship_id, $friendship ) { // Send your email here. }, 10, 2 );
Perfect! This is exactly what I was looking for! 🙂
Thanks
How can i catch the id of the person (initiator) who send me the request?!
Got it! 🙂 $friendship->initiator_user_id
This is exactly what I am wanting to do. Can anyone elaborate on this. @2611media how did you end up creating the email to send? I’m not sure how to move forward. Thanks in advance for any help.
Ok, I’ve got this worked out. The only issue I have now is getting the name of the user who denied the friendship into the email. Ex: ‘user’ denied your friendship. Anyone know how that could be done?
You have the id of that member:
$friendship->friend_user_id
Getting the name is done via WP functions.
For example: get_userdata@shanebp – Thanks. I’ve actually figured the entire thing out. Posting code below in case anyone else needs it.
function denial_email_creation() { // Do not create if it already exists and is not in the trash $post_exists = post_exists( '{{denier.name}} denied your friend request.' ); if ( $post_exists != 0 && get_post_status( $post_exists ) == 'publish' ) return; // Create post object $my_post = array( 'post_title' => __( '{{denier.name}} denied your friend request.', 'buddypress' ), 'post_content' => __( '<p>{{denier.name}} denied your friend request. And the rest of your email content.......</p>', 'buddypress' ), // HTML email content. 'post_excerpt' => __( '<p>{{denier.name}} denied your friend request. And the rest of your email content.......</p>', '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 'friendship_denied' // Email is a custom post type, therefore use wp_set_object_terms $tt_ids = wp_set_object_terms( $post_id, 'friendship_denied', 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' => 'Recipients Friendship Denied', ) ); } } } add_action( 'bp_core_install_emails', 'denial_email_creation' ); add_action( 'friends_friendship_rejected', function( $friendship_id, $friendship ) { $user_id = $friendship->initiator_user_id; $denier_id = $friendship->friend_user_id; $args = array( 'tokens' => array( 'site.name' => get_bloginfo('name'), 'denier.name' => bp_core_get_user_displayname( $denier_id ), ), ); bp_send_email( 'friendship_denied', $user_id, $args ); // Send your email here. }, 10, 2 );
Thanks for posting your solution – which has been updated with your correction.
- You must be logged in to reply to this topic.