Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanhellyer
Created February 5, 2016 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhellyer/c31bf993d55e1d7450a8 to your computer and use it in GitHub Desktop.
Save ryanhellyer/c31bf993d55e1d7450a8 to your computer and use it in GitHub Desktop.
BuddyPress add acitivity to groups - bug
<?php
class RR_BuddyPress {
public function __construct() {
add_post_type_support( 'events', 'buddypress-activity' );
add_action( 'bp_activity_before_save', array( $this, 'add_activity' ) );
}
/**
* Add activity.
*
* @param object $activity
*/
public function add_activity( $activity ) {
// Bail out if not saving an event
if ( 'new_events' != $activity->type ) {
return;
}
$post_id = $activity->secondary_item_id;
$current_user = wp_get_current_user();
$post = get_post( $post_id );
$excerpt = $post->post_excerpt;
if ( '' == $excerpt ) {
$excerpt = $post->post_content;
}
// Save the activity
$args = array(
// 'id' => '',
'action' => '<a href="' . esc_url( get_author_posts_url( $current_user->ID, $current_user->user_nicename ) ) . '">' . $current_user->display_name . '</a>' . __( ' did something', 'XXX' ),
'content' => $excerpt,
'component' => 'groups',
'type' => 'activity_update',
'item_id' => 2, // The group ID
'secondary_item_id' => $post_id, // Post ID
);
bp_activity_add( $args );
}
}
@imath
Copy link

imath commented Feb 5, 2016

I think i would do something like this.

function ryanhellyer_post_type_activities_in_group() {
    if ( ! bp_is_active( 'activity' ) ) {
        return;
    }

    add_post_type_support( 'page', 'buddypress-activity' );

    bp_activity_set_post_type_tracking_args( 'page', array(
        'action_id' => 'new_ryanhellyer_page',
    ) );
}
add_action( 'bp_init', 'ryanhellyer_post_type_activities_in_group' );

function ryanhellyer_activity_add( $args = array() ) {
    if ( empty( $args['type'] ) || 'new_ryanhellyer_page' !== $args['type'] ) {
        return $args;
    }

    // if posted in a group...
    if ( bp_is_group() ) {
        $args['component'] = 'groups';
        $args['item_id']   = groups_get_current_group()->id;
    }

    return $args;
}
add_filter( 'bp_before_activity_add_parse_args', 'ryanhellyer_activity_add', 10, 1 );

@ryanhellyer
Copy link
Author

Thanks, but I'm having trouble understanding this :/

That code makes the activity appear in the main feed, but it doesn't show up in an individual groups activity.

@ryanhellyer
Copy link
Author

I figured this out after taking another look at it today :) I tried adding groups_get_current_group()->id as a hard coded ID (since BuddyPress would have no way of knowing which group I wanted to assign). Today I realiseid I of course also needed to remove the conditional if bp_is_group() from around it too, and it worked :)

@ryanhellyer
Copy link
Author

I don't seem to need the bp_activity_set_post_type_tracking_args() when using it with a custom post-type, so I've been able to remove that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment