bp_activity_at_name_filter( string $content, int $activity_id )
Find and link @-mentioned users in the contents of a given item.
Description
Parameters
- $content
-
(Required) The contents of a given item.
- $activity_id
-
(Required) The activity id. Deprecated.
Return
(string) $content Content filtered for mentions.
Source
File: bp-activity/bp-activity-filters.php
function bp_activity_at_name_filter( $content, $activity_id = 0 ) {
// Are mentions disabled?
if ( ! bp_activity_do_mentions() ) {
return $content;
}
// Try to find mentions.
$usernames = bp_activity_find_mentions( $content );
// No mentions? Stop now!
if ( empty( $usernames ) )
return $content;
// We don't want to link @mentions that are inside of links, so we
// temporarily remove them.
$replace_count = 0;
$replacements = array();
foreach ( $usernames as $username ) {
// Prevent @ name linking inside <a> tags.
preg_match_all( '/(<a.*?(?!<\/a>)@' . $username . '.*?<\/a>)/', $content, $content_matches );
if ( ! empty( $content_matches[1] ) ) {
foreach ( $content_matches[1] as $replacement ) {
$replacements[ '#BPAN' . $replace_count ] = $replacement;
$content = str_replace( $replacement, '#BPAN' . $replace_count, $content );
$replace_count++;
}
}
}
// Linkify the mentions with the username.
foreach ( (array) $usernames as $user_id => $username ) {
$content = preg_replace( '/(@' . $username . '\b)/', "<a class='bp-suggestions-mention' href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content );
}
// Put everything back.
if ( ! empty( $replacements ) ) {
foreach ( $replacements as $placeholder => $original ) {
$content = str_replace( $placeholder, $original, $content );
}
}
// Return the content.
return $content;
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 1.2.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.