bp_find_mentions_by_at_sign( array $mentioned_users, string $content )

Locate usernames in an content string, as designated by an @ sign.

Description

Parameters

$mentioned_users

(Required) Associative array with user IDs as keys and usernames as values.

$content

(Required) Content

Return

(array|bool) Associative array with user ID as key and username as value. Boolean false if no mentions found.

Source

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

4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
function bp_find_mentions_by_at_sign( $mentioned_users, $content ) {
    $pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
    preg_match_all( $pattern, $content, $usernames );
 
    // Make sure there's only one instance of each username.
    $usernames = array_unique( $usernames[1] );
 
    // Bail if no usernames.
    if ( empty( $usernames ) ) {
        return $mentioned_users;
    }
 
    // We've found some mentions! Check to see if users exist.
    foreach ( (array) array_values( $usernames ) as $username ) {
        $user_id = bp_get_userid_from_mentionname( $username );
 
        // The user ID exists, so let's add it to our array.
        if ( ! empty( $user_id ) ) {
            $mentioned_users[ $user_id ] = $username;
        }
    }
 
    if ( empty( $mentioned_users ) ) {
        return $mentioned_users;
    }
 
    return $mentioned_users;
}

Changelog

Changelog
Version Description
BuddyBoss 1.2.8 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.