BuddyPress activity with WP User Frontend

A- A+

Let's say you want to post something via WPUF in your BuddyPress activity. How will you do that? Here's what we are going to do today:

What we'll do

  • Add a new custom field in the form
  • Whatever you put in the custom field will be posted as an activity in your BuddyPress profile
  • Also, it will be posted in a group you want

Add a new custom field

Screen Shot 2014-03-06 at 1.23.12 PM

Add a new custom field with a meta key bp_activity

Post an update

Screen Shot 2014-03-06 at 1.27.09 PM
Screen Shot 2014-03-06 at 1.29.17 PM

Now it looks like this:

Codes

Paste these codes into your themes functions.php.

[php] function wpuf_bp_add_user_activity( $activity_content ) {
global $bp;

$user_id = get_current_user_id();
$from_user_link = bp_core_get_userlink( $user_id );
$activity_action = sprintf( __( ‘%s posted an update', ‘buddypress' ), $from_user_link );
$primary_link = bp_core_get_userlink( $user_id, false, true );

$activity_id = bp_activity_add( array(
‘user_id' => $user_id,
‘action' => $activity_action,
‘content' => $activity_content,
‘primary_link' => $primary_link,
‘component' => $bp->activity->id,
‘type' => ‘activity_update'
) );

return $activity_id;
}

function wpuf_bp_add_group_activity( $group_id, $content ) {
$user_id = get_current_user_id();

$activity_id = groups_post_update( array(
‘user_id' => $user_id,
‘group_id' => $group_id,
‘content' => $content
) );

return $activity_id;
}

function wpufe_add_bp_activities( $post_id ) {

if ( isset( $_POST[‘bp_activity']) ) {
$content = trim( $_POST[‘bp_activity'] );

if ( !strlen( $content ) ) {
return;
}

$group_id = 1;

wpuf_bp_add_user_activity( $content );
wpuf_bp_add_group_activity( $group_id, $content );
}

}

add_action( ‘wpuf_add_post_after_insert', ‘wpufe_add_bp_activities' );
[/php]

Note: Here in the code, the $group_id is static. Change the group ID to your desired group ID.