bbp_add_user_favorite

Add a topic to user's favorites.

Description

bbp_add_user_favorite( (int) $user_id = 0, (int) $topic_id = 0 ); 

Parameters (2)

0. $user_id — Optional. (int)
User id
1. $topic_id — Optional. (int)
Topic id

Usage

  1. if ( !function_exists( 'bbp_add_user_favorite' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/users/functions.php'; 
  3.  
  4. // Optional. User id 
  5. $user_id = -1; 
  6.  
  7. // Optional. Topic id 
  8. $topic_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_add_user_favorite($user_id, $topic_id); 
  12.  

Defined (1)

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

/includes/users/functions.php  
  1. function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) { 
  2. if ( empty( $user_id ) || empty( $topic_id ) ) 
  3. return false; 
  4.  
  5. $topic = bbp_get_topic( $topic_id ); 
  6. if ( empty( $topic ) ) 
  7. return false; 
  8.  
  9. $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 
  10. if ( !in_array( $topic_id, $favorites ) ) { 
  11. $favorites[] = $topic_id; 
  12. $favorites = implode( ', ', wp_parse_id_list( array_filter( $favorites ) ) ); 
  13. update_user_option( $user_id, '_bbp_favorites', $favorites ); 
  14.  
  15. do_action( 'bbp_add_user_favorite', $user_id, $topic_id ); 
  16.  
  17. return true;