bp_activity_add

Add an activity item.

Description

(int|bool) bp_activity_add( (string) $args = '' ); 

Returns (int|bool)

The ID of the activity on success. False on error.

Parameters (1)

0. $args — Optional. (string) => ''
An array of arguments.

Options

  • id (int|bool) => 0

    Pass an activity ID to update an existing item, or false to create a new item. Default: false.

array(

    /**
     * Pass an activity ID to update an existing item, or false to create a new item. Default: false.
     *
     * @type int|bool
     */
    'id' => 0
);        


Usage

  1. if ( !function_exists( 'bp_activity_add' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // An array of arguments. 
  5. $args = array( 
  6. 'id' => 0 
  7. ); 
  8.  
  9. // NOTICE! Understand what this does before running. 
  10. $result = bp_activity_add($args); 
  11.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_add( $args = '' ) { 
  2.  
  3. $r = bp_parse_args( $args, array( 
  4. 'id' => false, // Pass an existing activity ID to update an existing entry. 
  5. 'action' => '', // The activity action - e.g. "Jon Doe posted an update" 
  6. 'content' => '', // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!" 
  7. 'component' => false, // The name/ID of the component e.g. groups, profile, mycomponent. 
  8. 'type' => false, // The activity type e.g. activity_update, profile_updated. 
  9. 'primary_link' => '', // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink). 
  10. 'user_id' => bp_loggedin_user_id(), // Optional: The user to record the activity for, can be false if this activity is not for a user. 
  11. 'item_id' => false, // Optional: The ID of the specific item being recorded, e.g. a blog_id. 
  12. 'secondary_item_id' => false, // Optional: A second ID used to further filter e.g. a comment_id. 
  13. 'recorded_time' => bp_core_current_time(), // The GMT time that this activity was recorded. 
  14. 'hide_sitewide' => false, // Should this be hidden on the sitewide activity stream? 
  15. 'is_spam' => false, // Is this activity item to be marked as spam? 
  16. 'error_type' => 'bool' 
  17. ), 'activity_add' ); 
  18.  
  19. // Make sure we are backwards compatible. 
  20. if ( empty( $r['component'] ) && !empty( $r['component_name'] ) ) { 
  21. $r['component'] = $r['component_name']; 
  22.  
  23. if ( empty( $r['type'] ) && !empty( $r['component_action'] ) ) { 
  24. $r['type'] = $r['component_action']; 
  25.  
  26. // Setup activity to be added. 
  27. $activity = new BP_Activity_Activity( $r['id'] ); 
  28. $activity->user_id = $r['user_id']; 
  29. $activity->component = $r['component']; 
  30. $activity->type = $r['type']; 
  31. $activity->content = $r['content']; 
  32. $activity->primary_link = $r['primary_link']; 
  33. $activity->item_id = $r['item_id']; 
  34. $activity->secondary_item_id = $r['secondary_item_id']; 
  35. $activity->date_recorded = $r['recorded_time']; 
  36. $activity->hide_sitewide = $r['hide_sitewide']; 
  37. $activity->is_spam = $r['is_spam']; 
  38. $activity->error_type = $r['error_type']; 
  39. $activity->action = ! empty( $r['action'] ) 
  40. ? $r['action'] 
  41.  
  42. $save = $activity->save(); 
  43.  
  44. if ( wp_error === $r['error_type'] && is_wp_error( $save ) ) { 
  45. return $save; 
  46. } elseif ('bool' === $r['error_type'] && false === $save ) { 
  47. return false; 
  48.  
  49. // If this is an activity comment, rebuild the tree. 
  50. if ( 'activity_comment' === $activity->type ) { 
  51. // Also clear the comment cache for the parent activity ID. 
  52. wp_cache_delete( $activity->item_id, 'bp_activity_comments' ); 
  53.  
  54. BP_Activity_Activity::rebuild_activity_comment_tree( $activity->item_id ); 
  55.  
  56. wp_cache_delete( 'bp_activity_sitewide_front', 'bp' ); 
  57.  
  58. /** 
  59. * Fires at the end of the execution of adding a new activity item, before returning the new activity item ID. 
  60. * @since 1.1.0 
  61. * @param array $r Array of parsed arguments for the activity item being added. 
  62. */ 
  63.  
  64. return $activity->id;