Skip to:
Content
Pages
Categories
Search
Top
Bottom

manual bp_activity_add within bp_activity_before_save


  • sk_tamilan
    Participant

    @sk_tamilan

    WordPress Version 4.1
    BuddyPress Version Version 2.1.1

    I am trying to create a group activity feed everytime a new custom post type post is created.

    add_action('bp_activity_before_save', 'rewards_activity_content', 3);

    function rewards_activity_content($activity){
        global $post;
        global $bp;
    
           //i have some working code here
    
            $bp->groups->current_group = groups_get_group( array( 'group_id' => $group_id ) );
    
            $groups_activity_action  = sprintf( __( '%1$s posted a reward %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( $user_id ),'<a href="' . $post_permalink . '">' . $post->post_title . '</a>' ,'<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    
            $groups_args = array(
                'action'            => apply_filters( 'groups_activity_new_update_action',  $groups_activity_action  ),
                'content'           => apply_filters( 'groups_activity_new_update_content', $activity_content ),
                'component'         => 'groups',
                'type'              => 'activity_update',
                'primary_link'      => $post_permalink,
                'secondary_item_id' => $post_id,
                'item_id'           => $group_id,
                'user_id'           => $user_id,
            );
    
    do_action( 'bp_activity_add', $groups_args ); //not working
    
    }

    all of the values within the $group array is being populated its just the last do_action is not working. I also tried doing groups_record_activity.

    I have also tried to pass through these arguements through a custom action to bp_activity_add and its still not working any help would be grateful.

Viewing 3 replies - 1 through 3 (of 3 total)

  • modemlooper
    Moderator

    @modemlooper

    This is wrong. You add action to blog post publish and then call bp activity add in your function

    add_action(‘publish_{custom_post_type_name}’, ‘function_to_perform’);


    sk_tamilan
    Participant

    @sk_tamilan

    @modemlooper The only problem is i am updating the content with the Post ID and thumbnail of the post for activity prior to submitting it, is there any other way of doing is ?

    
    add_action('bp_activity_before_save', 'rewards_activity_content', 3);
    
    add_action('publish_rewards', 'groups_rewards_activity',4,1);
    
    /* Filter for content for post type reward before saved to database */
    function rewards_activity_content($activity){
    
        global $post;
        global $bp;
    
        if ($post->post_type == 'rewards') {
    
            $activity_id = (int) $activity->id;
            $post_id = (int) $activity->secondary_item_id;
            $user_id = (int) $activity->user_id;
            
    
            $post_type = get_post_type( $post_id );
            $thumbnail = get_the_post_thumbnail($post_id);
            $post_permalink = get_permalink($post_id);
            $title = get_the_title($post_id);
    
      
    
            $activity_content  = "<div class='post-id'>The post ID : {$post_id}</div>\n";
            $activity_content .= "<div class='thumbnail'>{$thumbnail}</div>\n";
            $activity->content = $activity_content;
    
            $group_id = (int) BP_Groups_Group::group_exists($field_group[0]);  //working :)
    
            
        
            $bp->groups->current_group = groups_get_group( array( 'group_id' => $group_id ) );
    
            $groups_activity_action  = sprintf( __( '%1$s posted a reward %2$s in the group %3$s', 'buddypress'), bp_core_get_userlink( $user_id ),'<a href="' . $post_permalink . '">' . $post->post_title . '</a>' ,'<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    
            $groups_args = array(
                'action'            => apply_filters( 'groups_activity_new_update_action',  $groups_activity_action  ),
                'content'           => apply_filters( 'groups_activity_new_update_content', $activity_content ),
                'component'         => 'groups',
                'type'              => 'activity_update',
                'type'              => 'new_forum_post',
                'primary_link'      => $post_permalink,
                'secondary_item_id' => $post_id,
                'item_id'           => $group_id,
                'user_id'           => $user_id,
            ); //all of groups_args array value is being populated
    
            //do_action( 'bp_activity_add', $groups_args ); //not working
            do_action( 'groups_rewards_activity', $groups_args );
    
        }
    
        return $activity; 
    }
    
    function groups_rewards_activity($groups_args){
    
       $id = bp_activity_add($groups_args);
    
       return $id;
    
    }

    sk_tamilan
    Participant

    @sk_tamilan

    hi i got it working i eventually understood this process thanks @modemlooper
    for reference i used this as well 🙂 https://gist.github.com/MartinL83/3787343

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘manual bp_activity_add within bp_activity_before_save’ is closed to new replies.
Skip to toolbar