bb_add_user_favorite

The BuddyPress bb add user favorite function.

Description

bb_add_user_favorite( $user_id, $topic_id ); 

Parameters (2)

0. $user_id
The user id.
1. $topic_id
The topic id.

Usage

  1. if ( !function_exists( 'bb_add_user_favorite' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-users.php'; 
  3.  
  4. // The user id. 
  5. $user_id = null; 
  6.  
  7. // The topic id. 
  8. $topic_id = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_add_user_favorite($user_id, $topic_id); 
  12.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-users.php  
  1. function bb_add_user_favorite( $user_id, $topic_id ) { 
  2. global $bbdb; 
  3. $user_id = (int) $user_id; 
  4. $topic_id = (int) $topic_id; 
  5. $user = bb_get_user( $user_id ); 
  6. $topic = get_topic( $topic_id ); 
  7. if ( !$user || !$topic ) 
  8. return false; 
  9.  
  10. $favorites_key = $bbdb->prefix . 'favorites'; 
  11. $fav = $user->$favorites_key ? explode(', ', $user->$favorites_key) : array(); 
  12. if ( ! in_array( $topic_id, $fav ) ) { 
  13. $fav[] = $topic_id; 
  14. $fav = implode(', ', $fav); 
  15. bb_update_usermeta( $user->ID, $favorites_key, $fav ); 
  16. do_action('bb_add_user_favorite', $user_id, $topic_id); 
  17. return true;