Sabuj Kundu 1st Apr 2019

Sometimes we need to integrate 3rd party plugins or custom plugins with buddyPpress. A common use is like “After user does X post an activity to buddyPress on behalf of that user”. A more specific example could be with our CBX Poll plugin. In CBX Poll plugin we have close integration with buddyPress – when user creates a poll there is an activity auto post to buddyPress on behalf of that user, same way while user votes a poll which increase engagement in buddyPress. In this blog post I will try to write details how we can implement such buddyPress posting from any custom plugin.

Poll vote notification

Posting to BuddyPress

To post in buddyPress there is a function ready and it’s name is bp_activity_add($buddy_post) and you need to prepare an array with necessary properties for variable $buddy_post.
The below code example will help to understand how we used to post in buddyPress activity for user votes poll notification.

	<code class="language-php">
$buddy_post = array(
	'id'                => false,
	// Pass an existing activity ID to update an existing entry.
	'action'            => sprintf(__( '%s has voted poll: <a target="_blank" href="%s" rel="noopener">%s</a>', 'cbxpollproaddon' ), bp_core_get_userlink( bp_loggedin_user_id() ), esc_url(get_permalink( $poll_id )), esc_attr(get_the_title( $poll_id )) ),
	// The activity action - e.g. "Jon Doe posted an update"
	'content'           => apply_filters( 'cbxpoll_buddypress_vote_usernote', __( '<blockquote>I have voted on this poll, why not you ?</blockquote>', 'cbxpollproaddon' ), $user_id, $poll_id ),
	'component'         => 'cbxpoll',
	// The name/ID of the component e.g. groups, profile, mycomponent
	'type'              => 'cbxpoll_vote',
	// 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'           => $user_id,
	// Optional: The user to record the activity for, can be false if this activity is not for a user.
	'item_id'           => $poll_id,
	// poll post id
	'secondary_item_id' => $vote_id,
	// recorded vote 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 stream?
	'is_spam'           => false,
	// Is this activity item to be marked as spam?
);

bp_activity_add( $buddy_post );
	</code>

https://gist.github.com/manchumahara/9ba8eb69305befdd0ba0858548debd51

Check more details in buddyPress codex about bp_activity_add

Extra Fields Posting in BuddyPress Activity Submit Form