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

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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

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.