In an online community, unanswered friend requests can quickly pile up, leading to missed connections and less engagement. If you’re running BuddyBoss, you already know it provides excellent social features. But what if users forget about pending requests for days on end?
In this guide, you’ll learn how to:
- Scan daily for pending friend requests older than 3 days.
- Send a single email + notification to each user with such requests, preventing spam.
- Integrate this logic into BuddyBoss’s native email/notification system so you can manage it from the WordPress admin.
The result? A more active, engaged community where friend requests don’t fall through the cracks.
1. Understanding the BuddyBoss Email “Situation”
Before we dive into the code, let’s clarify how BuddyBoss handles emails. BuddyBoss (and BuddyPress) uses a concept called an “Email Situation.” Think of it as a “trigger” or “scenario” that defines:
- When the email is sent (e.g., “A user is mentioned,” “A user’s request is accepted,” etc.).
- What the default subject, body, and plain-text content look like.
- Which tokens (placeholders like
{{poster.name}}
) can be merged into the email content.
Why it’s helpful: Once you define a custom situation, you’ll see it in BuddyBoss → Emails, where admins can edit the email subject, body, or design—just like built-in BuddyBoss emails.
2. The Daily Cron Logic
Cron (short for “chronograph”) is a scheduling mechanism. In WordPress, WP-Cron runs automatically when your site gets traffic, triggering scheduled tasks. We’ll use it to:
- Once a day, query the database for friend requests that are still pending (
is_confirmed = 0
) and older than 3 days (date_created < (now - 3 days)
). - Group these requests by the user who needs to respond.
- Send a single consolidated notification + email to that user—only if we haven’t emailed them in the last 3 days.
Result: No spamming. If they ignore the requests for multiple days, they’ll get one reminder every 3 days until they accept or reject.
3. The Notification System
BuddyBoss extends BuddyPress’s in-app notification feature. Usually, you see these notifications in the user’s toolbar or notifications menu. Our code will:
- Add a custom notification type with
register_notification_type()
—this is the “3-Day Friend Requests” type. - Fire a new notification with
bp_notifications_add_notification()
whenever we detect old requests. - Link that notification to a custom page (or the default friend requests page) so the user can see and act on them.
Why it’s important: Some users check their notifications more often than email, so hitting both channels ensures maximum reach.
4. Putting It All Together (Step-by-Step)
Below is a high-level overview of the code you’ll include in a single-file plugin (e.g., buddyboss-3day-friend-requests.php
). We’ll break it into logical sections:
4.1 Plugin Header & Basic Setup
/**
* Plugin Name: BuddyBoss 3-Day Friend Request Reminder
* Description: Sends a notification & email if a user has friend requests older than 3 days (only once every 3 days).
* Version: 1.0
* Text Domain: buddyboss-3day-requests
*/
- Plugin Header: Tells WordPress your plugin’s name, description, and text domain (for translations).
- Activating it in WordPress will run our hooks.
4.2 Activation Hook
register_activation_hook( __FILE__, 'bb_3day_requests_plugin_activation_check' );
function bb_3day_requests_plugin_activation_check() {
if ( ! class_exists( 'BP_Core_Notification_Abstract' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die(
__( 'BuddyBoss Platform must be active before activating this plugin.', 'buddyboss-3day-requests' )
);
}
if ( ! wp_next_scheduled( 'bb_3day_friend_check_event' ) ) {
wp_schedule_event( time(), 'daily', 'bb_3day_friend_check_event' );
}
}
- BuddyBoss Check: If BuddyBoss isn’t active, we bail.
- Daily Cron: Schedules
bb_3day_friend_check_event
once a day.
4.3 Deactivation Hook
register_deactivation_hook( __FILE__, 'bb_3day_requests_plugin_deactivation' );
function bb_3day_requests_plugin_deactivation() {
wp_clear_scheduled_hook( 'bb_3day_friend_check_event' );
}
- Cleanup: Prevents leftover cron tasks when your plugin is turned off.
4.4 Defining the Notification Class
add_action( 'bp_loaded', 'bb_3day_requests_plugin_init' );
function bb_3day_requests_plugin_init() {
if ( ! class_exists( 'BP_Core_Notification_Abstract' ) ) {
return;
}
class BB_ThreeDay_Requests_Notification extends BP_Core_Notification_Abstract {
public function load() {
// 1. Notification group
$this->register_notification_group(
'bb_3day_requests_group',
__( '3-Day Friend Requests', 'buddyboss-3day-requests' ),
__( '3-Day Friend Requests (Admin)', 'buddyboss-3day-requests' ),
5
);
// 2. Notification type
$this->register_notification_type(
'bb_3day_requests_type',
__( 'Friend Requests Pending for 3 Days', 'buddyboss-3day-requests' ),
'',
'bb_3day_requests_group',
true,
false,
''
);
// 3. Email situation
$this->register_email_type(
'bb-3day-email-key',
array(
'email_title' => __( '[{{{site.name}}}] Friend requests pending for 3 days!', 'buddyboss-3day-requests' ),
'email_content' => __( 'Hello <a href="{{{poster.url}}}">{{poster.name}}</a>, you have friend requests older than 3 days:<br><br>{{{custom_data}}}', 'buddyboss-3day-requests' ),
'email_plain_content' => __( 'Hello {{poster.name}}, you have friend requests older than 3 days:\n\n{{{custom_data}}}', 'buddyboss-3day-requests' ),
'situation_label' => __( 'User has friend requests older than 3 days', 'buddyboss-3day-requests' ),
'unsubscribe_text' => __( 'You will no longer receive these reminder emails.', 'buddyboss-3day-requests' ),
),
'bb_3day_requests_type'
);
// 4. In-app notification storage
$this->register_notification(
'bb_3day_requests_component',
'bb_3day_requests_action',
'bb_3day_requests_type',
'bb-icon-f bb-icon-bell'
);
}
public function format_notification(
$content, $item_id, $secondary_item_id, $total_items,
$component_action_name, $component_name, $notification_id, $screen
) {
$link = home_url( '/3day-requests-page' );
$text = __( 'You have friend requests older than 3 days! Click here.', 'buddyboss-3day-requests' );
if ( 'string' === $screen ) {
return '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>';
} else {
return array( 'link' => $link, 'text' => $text );
}
}
}
BB_ThreeDay_Requests_Notification::instance()->start();
}
Key Points:
register_email_type()
defines the “Email Situation” for BuddyBoss → Emails.register_notification_type()
sets up a “3-Day Friend Requests” type in BuddyBoss → Settings → Notifications.format_notification()
returns the link/text for the in-app notice.
4.5 Triggering the Notification + Email
function bb_3day_requests_trigger_notification( $user_id, $pending_list = array() ) {
bp_notifications_add_notification( array(
'user_id' => $user_id,
'item_id' => 123,
'secondary_item_id' => 0,
'component_name' => 'bb_3day_requests_component',
'component_action' => 'bb_3day_requests_action',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
) );
bp_send_email(
'bb-3day-email-key',
$user_id,
array(
'tokens' => array(
'poster.name' => bp_core_get_user_displayname( $user_id ),
'poster.url' => bp_core_get_user_domain( $user_id ),
'custom_data' => implode( '<br>', $pending_list ),
),
)
);
update_user_meta( $user_id, 'bb_3day_notif_last_sent', current_time( 'timestamp' ) );
}
What Happens:
bp_notifications_add_notification()
: Creates a new BuddyBoss in-app notification.bp_send_email()
: Fires off the “3-day requests” email using the tokens we pass.update_user_meta()
: Records the timestamp so we only send again if 3 more days pass.
4.6 The Daily Cron Function
add_action( 'bb_3day_friend_check_event', 'bb_3day_requests_cron_function' );
function bb_3day_requests_cron_function() {
global $wpdb;
$friends_table = buddypress()->friends->table_name;
$older_than_date = gmdate( 'Y-m-d H:i:s', strtotime( '-3 days' ) );
$sql = $wpdb->prepare(
"SELECT id, initiator_user_id, friend_user_id, date_created
FROM {$friends_table}
WHERE is_confirmed = 0
AND date_created < %s",
$older_than_date
);
$results = $wpdb->get_results( $sql );
if ( ! empty( $results ) ) {
$pending_requests = array();
foreach ( $results as $row ) {
$friend_user_id = (int) $row->friend_user_id;
$initiator_id = (int) $row->initiator_user_id;
$initiator_name = bp_core_get_user_displayname( $initiator_id );
$initiator_link = bp_core_get_user_domain( $initiator_id );
$link_html = sprintf(
'<a href="%s">%s</a>',
esc_url( $initiator_link ),
esc_html( $initiator_name )
);
if ( ! isset( $pending_requests[ $friend_user_id ] ) ) {
$pending_requests[ $friend_user_id ] = array();
}
$pending_requests[ $friend_user_id ][] = $link_html;
}
foreach ( $pending_requests as $user_id => $initiators_list ) {
$last_sent = get_user_meta( $user_id, 'bb_3day_notif_last_sent', true );
$can_send = true;
if ( $last_sent ) {
$diff = current_time( 'timestamp' ) - (int) $last_sent;
if ( $diff < 3 * DAY_IN_SECONDS ) {
$can_send = false;
}
}
if ( $can_send ) {
bb_3day_requests_trigger_notification( $user_id, $initiators_list );
}
}
}
}
Key Points:
- SQL Query: Looks in
bp_friends
foris_confirmed = 0
(pending) anddate_created
older than 3 days. - Grouping: We store each initiator link in
$pending_requests[ $friend_user_id ]
. - Check Timestamp: If the user was emailed in the last 3 days, skip. Otherwise, call our trigger function.
5. Testing & Tweaks
- Change
-3 days
to-10 minutes
for quick tests. - Manually run the cron function by hooking it into an admin page or using WP-CLI:
wp cron event run bb_3day_friend_check_event
- Shortcode: If you want a simple
[pending_requests_count]
on a page, you can write a small function to query how many 3-day-old requests the current user has.
Conclusion
By defining a custom email situation, hooking a daily cron job, and sending one notification/email per user, you ensure:
- No spam: The user only sees one reminder every 3 days if they still have old requests.
- Better engagement: Friend requests aren’t forgotten indefinitely.
- Admin control: You can edit the email’s subject/body in BuddyBoss → Emails and toggle the notification in BuddyBoss → Settings → Notifications.
Try it out, and watch your BuddyBoss community become more responsive, more engaged, and free of neglected friend requests!
Leave a Reply