bp_activity_get_userid_from_mentionname

Get a user ID from a "mentionname", the name used for a user in @-mentions.

Description

(int|bool) bp_activity_get_userid_from_mentionname( (string) $mentionname ); 

Returns (int|bool)

ID of the user, if one is found. Otherwise false.

Parameters (1)

0. $mentionname (string)
Username of user in @-mentions.

Usage

  1. if ( !function_exists( 'bp_activity_get_userid_from_mentionname' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // Username of user in @-mentions. 
  5. $mentionname = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_activity_get_userid_from_mentionname($mentionname); 
  9.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_get_userid_from_mentionname( $mentionname ) { 
  2. $user_id = false; 
  3.  
  4. /** 
  5. * In username compatibility mode, hyphens are ambiguous between 
  6. * actual hyphens and converted spaces. 
  7. * @todo There is the potential for username clashes between 'foo bar' 
  8. * and 'foo-bar' in compatibility mode. Come up with a system for 
  9. * unique mentionnames. 
  10. */ 
  11. // First, try the raw username. 
  12. $userdata = get_user_by( 'login', $mentionname ); 
  13.  
  14. // Doing a direct query to use proper regex. Necessary to 
  15. // account for hyphens + spaces in the same user_login. 
  16. if ( empty( $userdata ) || ! is_a( $userdata, 'WP_User' ) ) { 
  17. global $wpdb; 
  18. $regex = esc_sql( str_replace( '-', '[ \-]', $mentionname ) ); 
  19. $user_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->users} WHERE user_login REGEXP '{$regex}'" ); 
  20. } else { 
  21. $user_id = $userdata->ID; 
  22.  
  23. // When username compatibility mode is disabled, the mentionname is 
  24. // the same as the nicename. 
  25. } else { 
  26. $user_id = bp_core_get_userid_from_nicename( $mentionname ); 
  27.  
  28.  
  29. return $user_id;