bp_activity_update_mention_count_for_user( int $user_id, int $activity_id, string $action = 'add' )

Update the mention count for a given user.

Description

This function should be used when you’ve already parsed your activity item for @mentions.

Parameters

$user_id

(Required) The user ID.

$activity_id

(Required) The unique ID for the activity item.

$action

(Optional) 'delete' or 'add'. Default: 'add'.

Default value: 'add'

Return

(bool)

Source

File: bp-activity/bp-activity-functions.php

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) {
 
    if ( empty( $user_id ) || empty( $activity_id ) ) {
        return false;
    }
 
    // Adjust the mention list and count for the member.
    $new_mention_count = (int) bp_get_user_meta( $user_id, 'bp_new_mention_count', true );
    $new_mentions      =       bp_get_user_meta( $user_id, 'bp_new_mentions',      true );
 
    // Make sure new mentions is an array.
    if ( empty( $new_mentions ) ) {
        $new_mentions = array();
    }
 
    switch ( $action ) {
        case 'delete' :
            $key = array_search( $activity_id, $new_mentions );
 
            if ( $key !== false ) {
                unset( $new_mentions[$key] );
            }
 
            break;
 
        case 'add' :
        default :
            if ( !in_array( $activity_id, $new_mentions ) ) {
                $new_mentions[] = (int) $activity_id;
            }
 
            break;
    }
 
    // Get an updated mention count.
    $new_mention_count = count( $new_mentions );
 
    // Resave the user_meta.
    bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count );
    bp_update_user_meta( $user_id, 'bp_new_mentions',      $new_mentions );
 
    return true;
}

Changelog

Changelog
Version Description
BuddyPress 1.7.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.