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
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
| 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.