Adding custom post type to BuddyPress activity feeds

Hello!

So I'm trying to add a custom post type I created to my BuddyPress activity feeds.
I used CustomPress to make the "blog_post" type. (This differs from the regular main blog posts as it's just for members -- I'm using BuddyDev's "BuddyBlog" plugin so members can have blogs on their profiles).

I'd like for new posts to show up in the activity feeds. I added the following code (that I saw somewhere online) to my functions.php file:
add_post_type_support( 'blog_post', 'buddypress-activity' );

And it works! The activity now shows up in feeds. However, it looks like this:

[member username] wrote a new item 1 minute ago

How can I change "item" to say "Blog Post" instead?

Also, instead of the TITLE of the post following this, the first few lines of the actual post itself show up instead. (Along with the featured image, which is good). How can I replace the first couple of lines with the actual title of the blog post?

Any help or suggestions would be greatly appreciated. Thanks!

  • Michael Bissett

    Hey @Anton, Michael here! :slight_smile:

    Hope you're doing well today, been a while since we've bumped into each other here on the forums.

    The BuddyPress codex has a handy entry that should help answer some of what you're asking here:

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

    In terms of adding in the featured image, you may wish to have a look at this code here:

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

    Let me know if this helps please! :slight_smile:

    Kind Regards,
    Michael

  • Anton

    Thanks @Michael Bissett!

    The BuddyPress codex has a handy entry that should help answer some of what you're asking here:

    That's a bit over my head, but it looks like I should be copying the code from the section "Customizing generic activity attributes"? They have:

    // Don't forget to add the 'buddypress-activity' support!
    add_post_type_support( 'page', '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( 'page', array(
            'component_id'             => buddypress()->blogs->id,
            'action_id'                => 'new_blog_page',
            'bp_activity_admin_filter' => __( 'Published a new page', 'custom-domain' ),
            'bp_activity_front_filter' => __( 'Pages', 'custom-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s posted a new <a href="%2$s">page</a>', 'custom-textdomain' ),
            'bp_activity_new_post_ms'  => __( '%1$s posted a new <a href="%2$s">page</a>, on the site %3$s', 'custom-textdomain' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'bp_init', 'customize_page_tracking_args' );

    They use "page" for their example, but I should replace that (in the two instances I see it) with my custom post type "blog_post" -- correct?
    Then the uppercases "Pages" is the front-end filter label, yes? I can replace that with "Blog Posts"

    The only things I'm unsure about are the component_id and action_id -- should I change anything there? Or custom-domain/custom-textdomain -- is that calling my own domain, or am I supposed to fill in something here?

    Sorry I'm coding-ignorant. Still learning a lot here....

    As for the featured images, actually...the first attempt didn't show the featured image in the feed, but the second attempt did show the featured image (this is before doing anything else, and only after adding that one line to my functions.php).

  • Michael Bissett

    Hey @Anton,

    They use "page" for their example, but I should replace that (in the two instances I see it) with my custom post type "blog_post" -- correct?
    Then the uppercases "Pages" is the front-end filter label, yes? I can replace that with "Blog Posts"

    Yes to both of your points here. :slight_smile:

    The only things I'm unsure about are the component_id and action_id -- should I change anything there? Or custom-domain/custom-textdomain -- is that calling my own domain, or am I supposed to fill in something here?

    In you case, you can remove the mentions of "component_id" and "action_id" from the code, it should resort to default values then.

    As for the mentions of that custom textdomain, as the article was originally intended for plugin developers, the idea is to insert the textdomain for the plugin.

    In your case though, I'd just insert "buddypress" there.

    As for the featured images, actually...the first attempt didn't show the featured image in the feed, but the second attempt did show the featured image (this is before doing anything else, and only after adding that one line to my functions.php).

    So it's all working as it should now? Or is there something more to this one? Just want to make sure I'm reading this right. :slight_smile:

    Kind Regards,
    Michael

  • Anton

    Thanks @Michael Bissett!

    In you case, you can remove the mentions of "component_id" and "action_id" from the code, it should resort to default values then.

    As for the mentions of that custom textdomain, as the article was originally intended for plugin developers, the idea is to insert the textdomain for the plugin.

    In your case though, I'd just insert "buddypress" there.

    Just to be clear, "buddypress" should replace "custom-domain" AND "custom-textdomain," or just the latter? (Actually -- I replaced all and it seems to work...)

    So YAY -- it works!! Now it's saying "[member username] posted a new blog post 1 minute ago"
    :slight_smile:

    I still wish it then displayed the Title, instead of the first lines of the post itself...but it's not the end of the world.

    So it's all working as it should now? Or is there something more to this one? Just want to make sure I'm reading this right. :slight_smile:

    Ugh, actually the featured image only showed up ONCE (the second test attempt), and now doesn't show up ever again. Not sure why it only showed up that one time (especially since I could have sworn there was no change in code between the first and second test attempts).

    I went back to the code in the message of the link you provided above, but....it doesn't add the featured image either. Unless that's meant for regular blog posts, and I need to change something for my custom post type "blog_post"? (And by the way, am I messing things up by calling the new type simply "blog_post"?)

    Let me know....

  • Michael Bissett

    Hey @Anton,

    Just to be clear, "buddypress" should replace "custom-domain" AND "custom-textdomain," or just the latter? (Actually -- I replaced all and it seems to work...)

    Yep, replacing them both was just fine here. :slight_smile:

    I still wish it then displayed the Title, instead of the first lines of the post itself...but it's not the end of the world.

    The code in the "Customizing Post Type Activity Content" section in the article I mentioned mentions a code template that could be adjusted for this very purpose, this should work for you:

    function record_cpt_activity_content( $cpt ) {
    
        if ( 'new_blog_post' === $cpt['type'] ) {
    
            $cpt['content'] = get_the_title();
        }
    
        return $cpt;
    }
    add_filter('bp_after_activity_add_parse_args', 'record_cpt_activity_content');

    Ugh, actually the featured image only showed up ONCE (the second test attempt), and now doesn't show up ever again. Not sure why it only showed up that one time (especially since I could have sworn there was no change in code between the first and second test attempts).

    You should find success with this code (though depending on your theme displays images, you may want to use a bit of CSS code to display the image here as a block):

    function record_cpt_activity_content_featured_image( $cpt ) {
    
        if ( 'new_blog_post' === $cpt['type'] ) {
    
            global $wpdb, $post, $bp;
            $theimg = wp_get_attachment_image_src(  get_post_thumbnail_id( bp_get_activity_secondary_item_id() ) );
            $cpt['content'] .= '<img src="' . $theimg[0] . '">';
        }
    
        return $cpt;
    }
    add_filter('bp_after_activity_add_parse_args', 'record_cpt_activity_content_featured_image');

    The aforementioned CSS (should it be needed) looking like this:

    .activity-inner img {
        display: block;
    }

    Let me know if this helps please! :slight_smile:

    Kind Regards,
    Michael

  • Anton

    Hey @Michael Bissett!

    Thanks for this reply, though I think I may have done something wrong along the way. I added the above bits, but now I'm just getting the "[membername] posted a new blog post..." mention, but nothing else -- no title (or even excerpt), and no featured image.

    Here's what my bp-custom.php file looks like right now:

    <?php
    
    // Adds custom Blog Post Type to activity feed
    
    add_post_type_support( 'blog_post', '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( 'blog_post', array(
            'bp_activity_admin_filter' => __( 'Published a new Blog Post', 'buddypress' ),
            'bp_activity_front_filter' => __( 'Blog Posts', 'buddypress' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s posted a new <a href="%2$s">blog post</a>', 'buddypress' ),
            'bp_activity_new_post_ms'  => __( '%1$s posted a new <a href="%2$s">blog post</a>, on his profile', 'buddypress' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'bp_init', 'customize_page_tracking_args' );
    
    // Adds title of custom Blog Post Type instead of excerpt
    
    function record_cpt_activity_content( $cpt ) {
    
        if ( 'new_blog_post' === $cpt['type'] ) {
    
            $cpt['content'] = get_the_title();
        }
    
        return $cpt;
    }
    add_filter('bp_after_activity_add_parse_args', 'record_cpt_activity_content');
    
    // Adds custom blog featured image to activity feed
    
    function record_cpt_activity_content_featured_image( $cpt ) {
    
        if ( 'new_blog_post' === $cpt['type'] ) {
    
            global $wpdb, $post, $bp;
            $theimg = wp_get_attachment_image_src(  get_post_thumbnail_id( bp_get_activity_secondary_item_id() ) );
            $cpt['content'] .= '<img src="' . $theimg[0] . '">';
        }
    
        return $cpt;
    }
    add_filter('bp_after_activity_add_parse_args', 'record_cpt_activity_content_featured_image');
    
    ?>

    Did I do something wrong?
    Thanks

  • Michael Bissett

    Hey @Anton, thanks for your patience here! :slight_smile:

    Odd, it should be working inside of bp-custom.php (though I'd remove that closing ?> tag, that's not usually needed)... hmm.

    I'd like to have a closer look at things here, could you please send in the following for your site via our secure contact form:

    - Mark to my attention, the subject line should contain only: ATTN: Michael Bissett
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - Include WordPress admin access details (login address, username & password)
    - Include FTP log-in details (hostname, username & password)
    - Include any relevant URLs for your site

    On the contact form (linked to below), please select "I have a different question", this ensures it comes through and gets assigned to me.

    https://premium.wpmudev.org/contact/

    Thanks! :slight_smile:

    Kind Regards,
    Michael

  • Michael Bissett

    Hey @Anton, no problem, thanks for sending in those details! :slight_smile:

    I just tried publishing a test post from the "blog_post" post type on the test site you created, and I'm seeing that the title & featured image are being posted in the sitewide activity feed, as well as the user's personal activity feed (I created a post called "Test post!" from inside the Dashboard, and attached a dummy image).

    Are you seeing this as well if you try to repeat what I've done here? And is there a certain way that you're trying to publish here, that causes the featured image to not be pulled (along with the post title)?

    Please advise,
    Michael

  • Anton

    @Michael Bissett --

    I tried again twice (using different users), with no luck.
    And then I realized...maybe you were publishing from the backend?
    So I made a new post from the Admin menu in the dashboard (Blog Posts>Add New), and sure enough....the featured image and title finally showed. (If you see the Activity feed now, the latest post -- written via the backend -- shows up. But the previous two posts, between the last one and yours, show the activity mention with just "[user] posted a blog post" only).

    The backend is NOT meant to be seen on my site by regular members. Everything is "front-end"-only. So the way members will be writing blog posts is via their Profile, and the "Blog" tab (which in this theme gets hidden under the "---" dropdown), and then the "New Post" link.

    Is there a way to still get the full activity mentions when done this way?

    Thanks!

  • Michael Bissett

    Hey @Anton, thanks for your patience here! :slight_smile:

    I've had another look at this, but I'm not seeing anything about the code presently that'd cause a hiccup when trying to post on the front end (I did try changing the filter that's hooked into by the featured image & content changes from "bp_after_activity_add_parse_args" to "bp_before_activity_add_parse_args", to no avail).

    I'd want to contact the author of the plugin about this, and see if there's something else that's required for his plugin, in order for the activity updates to perform correctly. Like we found earlier, it's working if a post is published from the back-end, so we know it can be done there.

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.