bp_activity_at_name_filter

Find and link @-mentioned users in the contents of a given item.

Description

(string) bp_activity_at_name_filter( (string) $content, (int) $activity_id = 0 ); 

Returns (string)

$content Content filtered for mentions.

Parameters (2)

0. $content (string)
The contents of a given item.
1. $activity_id — Optional. (int)
The activity id. Deprecated.

Usage

  1. if ( !function_exists( 'bp_activity_at_name_filter' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-filters.php'; 
  3.  
  4. // The contents of a given item. 
  5. $content = ''; 
  6.  
  7. // The activity id. Deprecated. 
  8. $activity_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_activity_at_name_filter($content, $activity_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-activity/bp-activity-filters.php  
  1. function bp_activity_at_name_filter( $content, $activity_id = 0 ) { 
  2.  
  3. // Are mentions disabled? 
  4. if ( ! bp_activity_do_mentions() ) { 
  5. return $content; 
  6.  
  7. // Try to find mentions. 
  8. $usernames = bp_activity_find_mentions( $content ); 
  9.  
  10. // No mentions? Stop now! 
  11. if ( empty( $usernames ) ) 
  12. return $content; 
  13.  
  14. // We don't want to link @mentions that are inside of links, so we 
  15. // temporarily remove them. 
  16. $replace_count = 0; 
  17. $replacements = array(); 
  18. foreach ( $usernames as $username ) { 
  19. // Prevent @ name linking inside <a> tags. 
  20. preg_match_all( '/(<a.*?(?!<\/a>)@' . $username . '.*?<\/a>)/', $content, $content_matches ); 
  21. if ( ! empty( $content_matches[1] ) ) { 
  22. foreach ( $content_matches[1] as $replacement ) { 
  23. $replacements[ '#BPAN' . $replace_count ] = $replacement; 
  24. $content = str_replace( $replacement, '#BPAN' . $replace_count, $content ); 
  25. $replace_count++; 
  26.  
  27. // Linkify the mentions with the username. 
  28. foreach ( (array) $usernames as $user_id => $username ) { 
  29. $content = preg_replace( '/(@' . $username . '\b)/', "<a class='bp-suggestions-mention' href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content ); 
  30.  
  31. // Put everything back. 
  32. if ( ! empty( $replacements ) ) { 
  33. foreach ( $replacements as $placeholder => $original ) { 
  34. $content = str_replace( $placeholder, $original, $content ); 
  35.  
  36. // Return the content. 
  37. return $content;