bp_activity_find_mentions

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

Description

(array|bool) bp_activity_find_mentions( (string) $content ); 

Returns (array|bool)

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

Parameters (1)

0. $content (string)
The content of the activity, usually found in $activity->content.

Usage

  1. if ( !function_exists( 'bp_activity_find_mentions' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // The content of the activity, usually found in 
  5. // $activity->content. 
  6. $content = ''; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = bp_activity_find_mentions($content); 
  10.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_find_mentions( $content ) { 
  2.  
  3. $pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/'; 
  4. preg_match_all( $pattern, $content, $usernames ); 
  5.  
  6. // Make sure there's only one instance of each username. 
  7. $usernames = array_unique( $usernames[1] ); 
  8.  
  9. // Bail if no usernames. 
  10. if ( empty( $usernames ) ) { 
  11. return false; 
  12.  
  13. $mentioned_users = array(); 
  14.  
  15. // We've found some mentions! Check to see if users exist. 
  16. foreach( (array) array_values( $usernames ) as $username ) { 
  17. $user_id = bp_activity_get_userid_from_mentionname( $username ); 
  18.  
  19. // The user ID exists, so let's add it to our array. 
  20. if ( ! empty( $user_id ) ) { 
  21. $mentioned_users[ $user_id ] = $username; 
  22.  
  23. if ( empty( $mentioned_users ) ) { 
  24. return false; 
  25.  
  26. /** 
  27. * Filters the mentioned users. 
  28. * @since 2.5.0 
  29. * @param array $mentioned_users Associative array with user IDs as keys and usernames as values. 
  30. */ 
  31. return apply_filters( 'bp_activity_mentioned_users', $mentioned_users );