Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Post activity_action output wonky


  • majecdad
    Participant

    @majecdad

    Hey,

    After more hours than I care to admit, 🙂 I finally found a way to add a custom post to an activity stream without Site Tracking… thanks @shanebp for this.

    However, two issues came up… 1) the blurb in the SWA is not as expected, and 2) the Featured image never came through. I’m just trying to get an idea as to why.

    As referenced, the activity_action is: $activity_action = sprintf( __( '%1$s created a new Post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );

    But what shows up on the SWA is: “username wrote a new item” — where username links to the Author and the word item links to the post.

    I was expecting something along the lines of: “username created a new Post, post title” — where post title was the title of the post and its link.

    Any guidance on what I am missing to make these two things happen?

    Thanks!

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

  • Henry Wright
    Moderator

    @henrywright

    Hi @majecdad

    Considering your $activity_action should be “username created a new Post” but “username wrote a new item” is appearing, then your activity action is obviously being ignored.

    What’s your whole code?


    majecdad
    Participant

    @majecdad

    Hi @henrywright

    The code was that that I pulled from the other post. Here it is:

    add_filter('bp_blogs_activity_new_post_action', 'record_cpt_activity_action', 1, 3);
    function record_cpt_activity_action( $activity_action,  $post, $post_permalink ) { 
    
       if( $post->post_type == 'agency' ) {
    	$activity_action  = sprintf( __( '%1$s created a new Post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
       }
    	
       return $activity_action;
    }
    

    the only change was that I changed the post_type from ‘projects’ in the original to ‘agency’ in mine, and changed ‘Project’ to ‘Post’ in the next line.

    This is my first test use of CPT’s and then trying to tie them to the BP stream, so I’m learning. 🙂

    Thanks.


    Henry Wright
    Moderator

    @henrywright

    Thanks for posting your code. I’m pretty sure the bp_blogs_activity_new_post_action hook fires when you create an activity post, not when you create a new WP “post”


    majecdad
    Participant

    @majecdad

    Honestly, I have no idea. But it IS posting a new BP activity to the Stream when I have created a new custom post on the site… so that part is working.

    It is simply the description/permalink of that activity that was not posting as expected.

    I’m wondering if the if( $post->post_type == 'agency' ) needs to be the plural form? The CPT is set as agency, so that is what I used (but the original code was ‘projects’).

    I also wonder if the sprintf( __( '%1$s created a new Post is wrong, as the original code had the (singular) name of the CPT, where I changed this to Post – (thinking it was just text).

    I’ll try multiple changes to see what happens, but if anyone has a shortcut to “correct” 🙂 I’m all ears.

    Thanks.


    majecdad
    Participant

    @majecdad

    First, @henrywright, I think you were right, that code was not creating the entry, it was created because I had add_post_type_support( 'alert', 'buddypress-activity' ); in bp-custom too. Thanks for making me think twice.

    It’s amazing what you can learn in 12 hours. Not enough, but it’s a start. So I tossed the baby out with the bathwater, and read this about 15 times to get to a partial understanding of it. I am now using this:

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

    After about 20 different mods, adding and deleting and changing, I got a CPT to take to the activity stream, with the (mostly) modified text I was looking for.

    I actually don’t even know what some of the items in the array are, I just changed things until they worked. 🙂 In fact, I had to comment out component id, because I still haven’t figured out what goes there to keep from getting a WSOD.

    The only main thing I’d really still like to accomplish is getting the Post Title in the link that is created here: '%1$s posted an important new <a href="%2$s">** Alert **</a>' — where it would be like “soandso posted an important new Alert:The Title of the CPT”

    Any ideas out there?

    If this is still not the right/best way to accomplish this, I’m open to suggestion. But I think I am at least stepping in the right direction.

    Thanks.


    Henry Wright
    Moderator

    @henrywright

    @majecdad

    I did some digging and bp_blogs_activity_new_post_action does actually apply to blog posts so my apologies for leading you to believe it applies to activity posts.

    To answer your question about component_id, the docs say:

    ID of the BuddyPress component to associate the activity item

    This isn’t hugely helpful because it doesn’t tell us anything new. But the component_id is just the internal identifier of the component. It gets set during the component creation process. Take the Friends component as an example, see here. And in the case of blogs, see here. The buddypress()->blogs->id what you see in your function is just a way of accessing that ID.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Post activity_action output wonky’ is closed to new replies.
Skip to toolbar