1. Home
  2. Docs
  3. WP User Frontend Pro
  4. Tutorials
  5. BuddyPress activity with WP User Frontend

BuddyPress activity with WP User Frontend

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 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 meta key bp_activity

Post an update

Screen Shot 2014-03-06 at 1.27.09 PM

Now it looks like this:
Screen Shot 2014-03-06 at 1.29.17 PM

Codes

Paste this codes in your themes functions.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' );

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

Was this article helpful to you?

How can we help?