bp_activity_post_update

Post an activity update.

Description

(int|bool|WP_Error) bp_activity_post_update( (string) $args = '' ); 

Returns (int|bool|WP_Error)

$activity_id The activity id on success. On failure, either boolean false or WP_Error object depending on the 'error_type' $args parameter.

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

  • content (string) => ''

    The content of the activity update.

  • user_id (int) => to the logged-in user
array(

    /**
     * The content of the activity update.
     *
     * @type string
     * @default ''
     */
    'content' => '',

    /**
     * Optional.
     *
     * @type int
     * @default to the logged-in user
     * @optional
     */
    'user_id' => to the logged-in user
);        


Usage

  1. if ( !function_exists( 'bp_activity_post_update' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // The args. 
  5. $args = array( 
  6. 'content' => '', 
  7. 'user_id' => to the logged-in user 
  8. ); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_activity_post_update($args); 
  12.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_post_update( $args = '' ) { 
  2.  
  3. $r = wp_parse_args( $args, array( 
  4. 'content' => false,  
  5. 'user_id' => bp_loggedin_user_id(),  
  6. 'error_type' => 'bool',  
  7. ) ); 
  8.  
  9. if ( empty( $r['content'] ) || !strlen( trim( $r['content'] ) ) ) { 
  10. return false; 
  11.  
  12. if ( bp_is_user_inactive( $r['user_id'] ) ) { 
  13. return false; 
  14.  
  15. // Record this on the user's profile. 
  16. $activity_content = $r['content']; 
  17. $primary_link = bp_core_get_userlink( $r['user_id'], false, true ); 
  18.  
  19. /** 
  20. * Filters the new activity content for current activity item. 
  21. * @since 1.2.0 
  22. * @param string $activity_content Activity content posted by user. 
  23. */ 
  24. $add_content = apply_filters( 'bp_activity_new_update_content', $activity_content ); 
  25.  
  26. /** 
  27. * Filters the activity primary link for current activity item. 
  28. * @since 1.2.0 
  29. * @param string $primary_link Link to the profile for the user who posted the activity. 
  30. */ 
  31. $add_primary_link = apply_filters( 'bp_activity_new_update_primary_link', $primary_link ); 
  32.  
  33. // Now write the values. 
  34. $activity_id = bp_activity_add( array( 
  35. 'user_id' => $r['user_id'],  
  36. 'content' => $add_content,  
  37. 'primary_link' => $add_primary_link,  
  38. 'component' =>buddypress)->activity->id,  
  39. 'type' => 'activity_update',  
  40. 'error_type' => $r['error_type'] 
  41. ) ); 
  42.  
  43. // Bail on failure. 
  44. if ( false === $activity_id || is_wp_error( $activity_id ) ) { 
  45. return $activity_id; 
  46.  
  47. /** 
  48. * Filters the latest update content for the activity item. 
  49. * @since 1.6.0 
  50. * @param string $r Content of the activity update. 
  51. * @param string $activity_content Content of the activity update. 
  52. */ 
  53. $activity_content = apply_filters( 'bp_activity_latest_update_content', $r['content'], $activity_content ); 
  54.  
  55. // Add this update to the "latest update" usermeta so it can be fetched anywhere. 
  56. bp_update_user_meta( bp_loggedin_user_id(), 'bp_latest_update', array( 
  57. 'id' => $activity_id,  
  58. 'content' => $activity_content 
  59. ) ); 
  60.  
  61. /** 
  62. * Fires at the end of an activity post update, before returning the updated activity item ID. 
  63. * @since 1.2.0 
  64. * @param string $content Content of the activity post update. 
  65. * @param int $user_id ID of the user posting the activity update. 
  66. * @param int $activity_id ID of the activity item being updated. 
  67. */ 
  68. do_action( 'bp_activity_posted_update', $r['content'], $r['user_id'], $activity_id ); 
  69.  
  70. return $activity_id;