Adding custom post type to buddypress activity, the right approach


So, here I was working on  a buddypress based site trying to figure out a way to add the custom post type to my activity. After a lot of research and putting a whole of mind I finally figured out the most effective (at least for me) way to add a custom post type to buddypress activity.

So how to do it:

  1. First step is to make a child theme, trust me on this one, a child theme is the best practice you can adopt while working on an project, it allows you to make necessary customizations to your site and never gets disturbed when you update the parent theme, unless of-course you are making a parent theme ;). To know how to create a child theme refer the codex: https://codex.wordpress.org/Child_Themes
  2. Second step is to add a line to functions.php (which you have made while creating the child theme). So add this to functions.php:
    <?php 
    add_post_type_support( 'your_custom_post_type', 'buddypress-activity' );
     
    function customize_page_tracking_args() {
        // Check if the Activity component is active before using it.
        if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
     
        bp_activity_set_post_type_tracking_args( 'your_custom_post_type', array(
             'component_id'             => 'activity',
            'action_id'                => 'new_your_custom_post_type',
            'bp_activity_admin_filter' => __( 'Published a new badlee', 'custom-domain' ),
            'bp_activity_front_filter' => __( 'Your Custom Post Type Name', 'custom-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s posted a new <a href="%2$s">Custom Post</a>', 'custom-textdomain' ),
            'bp_activity_new_post_ms'  => __( '%1$s posted a new <a href="%2$s">Custom Post</a>, on the site %3$s', 'custom-textdomain' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'init', 'customize_page_tracking_args', 1000 );
    function record_cpt_activity_content( $cpt ) {
     
        if ( 'new_your_custom_post_type' === $cpt['type'] ) {
         
            $cpt['content'] = '&nbsp;';
        }
         
     
        return $cpt;
    }
    add_filter('bp_before_activity_add_parse_args', 'record_cpt_activity_content');
    ?>

    Note: Replace your_custom_post_type with your custom post type slug.
    So, by adding the afore-stated lines in the functions.php you are actually preparing the buddypress activity to record the posts you publish using your custom post type. So next time when you post the custom post type it will be recorded by the buddypress as one of your activity.

  3. Next step is to display the contents of the custom post type we made. To do so, we need to make changes to the loop of activity in buddypress. It is considerable that the loop exists in one of the template file of the buddypress(Plugin) i.e. entry.php, which we can not change because as soon as it will be updated, the changes will override with original file. So the trick is to create a separate directory in our child theme. To do so create a folder named “buddypress” in you child theme’s home directory. Create folder with name of “activity” inside buddypress folder. Now create “entry.php” in the activity folder and put following code in it:
    <?php
    
    /**
     * BuddyPress - Activity Stream (Single Item)
     *
     * This template is used by activity-loop.php and AJAX functions to show
     * each activity.
     *
     * @package BuddyPress
     * @subpackage bp-legacy
     */
    global $wpdb, $post, $bp;
    ?>
    
    <?php do_action( 'bp_before_activity_entry' ); ?>
    <?php echo $bp->current_item; ?>
    <li class="panel <?php bp_activity_css_class(); ?>" style="padding-top:0;" id="activity-<?php bp_activity_id(); ?>">
        <div class="panel-heading" style="padding-top:0;">
            <span class="activity-avatar">
                <a href="<?php bp_activity_user_link(); ?>">
                    <?php bp_activity_avatar(); ?>
                </a>
            </span>
                <?php  bp_activity_action(); ?>
        </div>
        <div class="panel-body activity-content">
            <?php if ( bp_activity_has_content() ) : ?>
    
                <div class="activity-inner" >
                    <?php //bp_activity_content_body(); ****Intentionally removed this to avoid the content writen from the functions.php file****
                    
                    $blogpost_id = bp_get_activity_secondary_item_id();
                    if ($blogpost_id) :
                        if (has_post_thumbnail( $blogpost_id ) )    :
                            $theimg = wp_get_attachment_image_src( get_post_thumbnail_id( $blogpost_id ), 'large' ); ?>
                        
                            <div class="badlee_img_container"><a href="<?php echo get_post_permalink($blogpost_id); ?>"> <img style="thumbnail" style="width:100%;" src="<?php echo $theimg[0]; ?>"></a></div>
                        <?php echo get_post_field('post_content', $blogpost_id); ?>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
    
            <?php endif; ?>
    
            <?php do_action( 'bp_activity_entry_content' ); ?>
    
            <div class="activity-meta">
                <?php if ( bp_get_activity_type() == 'activity_comment' ) : ?>
    
                    <a href="<?php bp_activity_thread_permalink(); ?>" type="button" class="button button-xs view" title="<?php _e( 'View Conversation', 'buddypress' ); ?>"><?php _e( 'All Comments', 'buddypress' ); ?></a>
    
                <?php endif; ?>
                <?php if ( is_user_logged_in() ) : ?>
                    <?php if ( bp_activity_can_comment() ) : ?>
                        <a href="<?php bp_activity_comment_link(); ?>" type="button" class="button button-xs acomment-reply"  id="acomment-comment-<?php bp_activity_id(); ?>"><i class="fa fa-comment"></i><?php printf( __( 'Comment <span>%s</span>', 'buddypress' ), bp_activity_get_comment_count() ); ?></a>
                    <?php endif; ?>
                    
                    <?php if ( bp_activity_can_favorite() ) : ?>
    
                        <?php if ( !bp_get_activity_is_favorite() ) : ?>
    
                            <a href="<?php bp_activity_favorite_link(); ?>" type="button" class="button button-xs fav"  title="<?php esc_attr_e( 'Mark as Favorite', 'buddypress' ); ?>"><i class="fa fa-thumbs-up"></i><?php _e( 'Favorite', 'buddypress' ); ?></a>
    
                        <?php else : ?>
    
                            <a href="<?php bp_activity_unfavorite_link(); ?>" class="button button-xs unfav bp-secondary-action"  title="<?php esc_attr_e( 'Remove Favorite', 'buddypress' ); ?>"><i class="fa fa-remove"></i><?php _e( 'UnFavorite', 'buddypress' ); ?></a>
    
                        <?php endif; ?>
    
                    <?php endif; ?>
                    
                    <?php if ( bp_activity_user_can_delete() ) {
                        echo '<a href="'.bp_get_activity_comment_delete_link().'" type="button" class="button button-xs item-button bp-secondary-action delete-activity confirm" rel="nofollow"><i class="fa fa-trash-o"></i>Remove</a>';
                        
                        } ?>
                    <?php do_action( 'bp_activity_entry_meta' ); ?>
                    
                <?php endif; ?>
            </div>
    
        </div>
    
        <?php do_action( 'bp_before_activity_entry_comments' ); ?>
    
        <?php if ( ( is_user_logged_in() && bp_activity_can_comment() ) || bp_activity_get_comment_count() ) : ?>
    
            <div class="activity-comments">
    
                <?php bp_activity_comments(); ?>
    
                <?php if ( is_user_logged_in() ) : ?>
    
                    <form action="<?php bp_activity_comment_form_action(); ?>" method="post" id="ac-form-<?php bp_activity_id(); ?>" class="ac-form"<?php bp_activity_comment_form_nojs_display(); ?>>
                        <div class="ac-reply-avatar"><?php bp_loggedin_user_avatar( 'width=' . BP_AVATAR_THUMB_WIDTH . '&height=' . BP_AVATAR_THUMB_HEIGHT ); ?></div>
                        <div class="ac-reply-content">
                            <div class="ac-textarea">
                                <textarea id="ac-input-<?php bp_activity_id(); ?>" class="ac-input" name="ac_input_<?php bp_activity_id(); ?>"></textarea>
                            </div>
                            <input type="submit" name="ac_form_submit" value="<?php _e( 'Post', 'buddypress' ); ?>" /> &nbsp; <a href="#" class="ac-reply-cancel"><?php _e( 'Cancel', 'buddypress' ); ?></a>
                            <input type="hidden" name="comment_form_id" value="<?php bp_activity_id(); ?>" />
                        </div>
    
                        <?php do_action( 'bp_activity_entry_comments' ); ?>
    
                        <?php wp_nonce_field( 'new_activity_comment', '_wpnonce_new_activity_comment' ); ?>
    
                    </form>
    
                <?php endif; ?>
    
            </div>
    
        <?php endif; ?>
    
        <?php do_action( 'bp_after_activity_entry_comments' ); ?>
    
    </li>
    
    <?php do_action( 'bp_after_activity_entry' ); ?>

    This will override the original entry.php code. By adding this code, you will be able to display the featured image and the content of the custom post. To add other meta data or content from the custom post you can use the usual wordpress functions by using “$blogpost_id” as post id. For Example the code below can be used to display the post title:

    <?php get_the_title($blogpost_id); ?>
  4. Thats it folks, do a good CSS and you are ready to go.. ENJOY!

References:

get_the_title()

https://codex.wordpress.org/Child_Themes

https://buddypress.org/support/topic/add-post-featured-image-in-activity-stream/

https://codex.buddypress.org/plugindev/post-types-activities/

 


One response to “Adding custom post type to buddypress activity, the right approach”

  1. OMG, thank you so much! I am like ready to die right now because it worked for my plugin. This is my first time following someone’s instructions and actually understanding it to get it to work. Thank you!

Leave a Reply

%d bloggers like this: