bp_activity_add( array|string $args = '' )

Add an activity item.

Description

Parameters

$args

(Optional) An array of arguments.

  • 'id'
    (int|bool) Pass an activity ID to update an existing item, or false to create a new item. Default: false.
  • 'action'
    (string) Optional. The activity action/description, typically something like "Joe posted an update". Values passed to this param will be stored in the database and used as a fallback for when the activity item's format_callback cannot be found (eg, when the component is disabled). As long as you have registered a format_callback for your $type, it is unnecessary to include this argument - BP will generate it automatically. See bp_activity_set_action().
  • 'content'
    (string) Optional. The content of the activity item.
  • 'component'
    (string) The unique name of the component associated with the activity item - 'groups', 'profile', etc.
  • 'type'
    (string) The specific activity type, used for directory filtering. 'new_blog_post', 'activity_update', etc.
  • 'primary_link'
    (string) Optional. The URL for this item, as used in RSS feeds. Defaults to the URL for this activity item's permalink page.
  • 'user_id'
    (int|bool) Optional. The ID of the user associated with the activity item. May be set to false or 0 if the item is not related to any user. Default: the ID of the currently logged-in user.
  • 'item_id'
    (int) Optional. The ID of the associated item.
  • 'secondary_item_id'
    (int) Optional. The ID of a secondary associated item.
  • 'date_recorded'
    (string) Optional. The GMT time, in Y-m-d h:i:s format, when the item was recorded. Defaults to the current time.
  • 'hide_sitewide'
    (bool) Should the item be hidden on sitewide streams? Default: false.
  • 'is_spam'
    (bool) Should the item be marked as spam? Default: false.
  • 'privacy'
    (string) Privacy of the activity Default: public.
  • 'error_type'
    (string) Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.

Default value: ''

Return

(WP_Error|bool|int) The ID of the activity on success. False on error.

Source

File: bp-activity/bp-activity-functions.php

function bp_activity_add( $args = '' ) {

	$r = bp_parse_args( $args, array(
		'id'                => false,                  // Pass an existing activity ID to update an existing entry.
		'action'            => '',                     // The activity action - e.g. "Jon Doe posted an update"
		'content'           => '',                     // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!"
		'component'         => false,                  // The name/ID of the component e.g. groups, profile, mycomponent.
		'type'              => false,                  // The activity type e.g. activity_update, profile_updated.
		'primary_link'      => '',                     // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink).
		'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.
		'item_id'           => false,                  // Optional: The ID of the specific item being recorded, e.g. a blog_id.
		'secondary_item_id' => false,                  // Optional: A second ID used to further filter e.g. a comment_id.
		'recorded_time'     => bp_core_current_time(), // The GMT time that this activity was recorded.
		'hide_sitewide'     => false,                  // Should this be hidden on the sitewide activity feed?
		'is_spam'           => false,                  // Is this activity item to be marked as spam?
		'privacy'           => 'public',               // privacy of the activity
		'error_type'        => 'bool'
	), 'activity_add' );

	// Make sure we are backwards compatible.
	if ( empty( $r['component'] ) && !empty( $r['component_name'] ) ) {
		$r['component'] = $r['component_name'];
	}

	if ( empty( $r['type'] ) && !empty( $r['component_action'] ) ) {
		$r['type'] = $r['component_action'];
	}

	// Setup activity to be added.
	$activity                    = new BP_Activity_Activity( $r['id'] );
	$activity->user_id           = $r['user_id'];
	$activity->component         = $r['component'];
	$activity->type              = $r['type'];
	$activity->content           = $r['content'];
	$activity->primary_link      = $r['primary_link'];
	$activity->item_id           = $r['item_id'];
	$activity->secondary_item_id = $r['secondary_item_id'];
	$activity->date_recorded     = $r['recorded_time'];
	$activity->hide_sitewide     = $r['hide_sitewide'];
	$activity->is_spam           = $r['is_spam'];
	$activity->privacy           = $r['privacy'];
	$activity->error_type        = $r['error_type'];
	$activity->action            = ! empty( $r['action'] )
						? $r['action']
						: bp_activity_generate_action_string( $activity );

	$save = $activity->save();

	if ( 'wp_error' === $r['error_type'] && is_wp_error( $save ) ) {
		return $save;
	} elseif ('bool' === $r['error_type'] && false === $save ) {
		return false;
	}

	// If this is an activity comment, rebuild the tree.
	if ( 'activity_comment' === $activity->type ) {
		// Also clear the comment cache for the parent activity ID.
		wp_cache_delete( $activity->item_id, 'bp_activity_comments' );

		BP_Activity_Activity::rebuild_activity_comment_tree( $activity->item_id );
	}

	wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );

	/**
	 * Fires at the end of the execution of adding a new activity item, before returning the new activity item ID.
	 *
	 * @since BuddyPress 1.1.0
	 *
	 * @param array $r Array of parsed arguments for the activity item being added.
	 */
	do_action( 'bp_activity_add', $r );

	return $activity->id;
}

Changelog

Changelog
Version Description
BuddyPress 2.6.0 Added 'error_type' parameter to $args. BuddyPress 2.6.0 Added 'error_type' parameter to $args.
BuddyPress 1.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.