Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Post Type Support for Activity


  • Monkey1980
    Participant

    @monkey1980

    Saw the 2.2 update today and was very pleased to see we can now have activity for Custom Post Types – so exited was I that I updated immediately!!! (coincidentally doing some dev’ stuff/maintenance)

    However my excitement soon passed – after poking around for the last few hours as the code you suggested did not seem to work fully it seems I have the following situation:

    1. When I specify my Custom Post Type, i do indeed get an activity entry …wahaay!!! – unfortunately the message is the default and not my custom message :0(

    2. If I specify the post type as ‘page’ – the message is my custom one (front end and back end) but obviously the activity feed is not updated as ‘page’ is not my CPT (previous entries from testing revealed this behaviour as when I changed the CPT back to page I have left the ‘action_id’ set to my CPT) :0(

    I also have a question, it seems the custom message %1$s for the user’s name – %2$s for the the link and %3$s for site name.

    For some reason %3$s doesn’t do anything?

    I would also prefer to show the page title as is shown when a blog is posted as supposed to just showing “User has posted a new ‘item'” – perhaps a %4$s can be added???

    Any help here would be most appreciated!! :0)

    Regards

    Sam

Viewing 25 replies - 1 through 25 (of 36 total)
  • Hi i’m sorry. It must be because i’m french, but i’m not sure to understand what you reported 🙁

    If i “rephrase” like this :

    When adding the buddypress-activity support for my custom post type and specifying custom action strings arguments i don’t manage to have my custom strings displayed into the activity stream but instead the default action string eg : user wrote a new item

    Would it be ok with your description of the trouble ?

    %3$s should do something if your config is multisite. Is your config multisite ?

    I would also prefer to show the page title as is shown when a blog is posted as supposed to just showing “User has posted a new ‘item’” – perhaps a %4$s can be added???

    This would require some more activity meta. I haven’t included this part in core, so you’ll need to add a filter to achieve your goal. But let’s try to fix things step by step. I’m going to investigate on the “rephrase” i’ve just done.

    My first impression is i’m a bit surprised as i’m using Post type activities for one of my plugin and everything seems to be ok… See this image :
    Ideastream


    Monkey1980
    Participant

    @monkey1980

    Sorry!

    Indeed, the New Post generates an activity item with ‘user wrote a new item’ – if a then change the CPT to ‘page’ but leave the action ID as my custom one the WordPress Admin and the Frontend both show my custom activity message! my code is below – the %3$s makes sense as I not running multi-site:

    // Don't forget to add the 'buddypress-activity' support!
    add_post_type_support( 'sd_article', '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( 'sd_article', array(
            'component_id'             => buddypress()->activity->id,
            'action_id'                => 'new_sd_article',
            'bp_activity_admin_filter' => __( 'Neuer Artikel veröffentlicht', 'custom-domain' ),
            'bp_activity_front_filter' => __( 'Artikel', 'custom-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'bp_activity_new_post'     => __( '%1$s hat den neuen <a href="%2$s">Artikel</a>', 'sozialdynamik' ),
            'bp_activity_new_post_ms'  => __( '%1$s hat den neuen <a href="%2$s">Artikel</a>, on the site %3$s', 'sozialdynamik' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'bp_init', 'customize_page_tracking_args' );
    
    ----->

    I hope this is clearer!! :0)

    1/ I don’t understand why you are switching to the page post type ?

    2/ Where is registered the sd_article post type ?

    3/ if you’re the one registering the post type, maybe you should define the BuddyPress activity support directly when registering it. eg:

    function plugin_registers_post_type() {
    	$args = array(
    		'public'   => true,
    		'labels'   => array(
    			'name'                     => __( 'Artikel', 'your-plugin-textdomain' ),
    			'singular_name'            => __( 'Artikel', 'sozialdynamik' ),
    			'bp_activity_admin_filter' => __( 'Neuer Artikel veröffentlicht', 'sozialdynamik' ),
    			'bp_activity_front_filter' => __( 'Artikel', 'sozialdynamik' ),
     			'bp_activity_new_post'     => __( '%1$s hat den neuen <a href="%2$s">Artikel</a>', 'sozialdynamik' ),
     			'bp_activity_new_post_ms'  => __( '%1$s hat den neuen <a href="%2$s">Artikel</a>, on the site %3$s', 'sozialdynamik' ),
    		),
    		'supports'    => array( 'title', 'editor', 'buddypress-activity' ),
    		'bp_activity' => array(
    			'component_id' => 'activity',
    			'action_id'    => 'new_sd_article',
    			'contexts'     => array( 'activity', 'member' ),
    			'position'     => 100,
    		),
    	);
    	register_post_type( 'sd_article', $args );
    }
    add_action( 'init', 'plugin_registers_post_type' );

    Just like it’s explained here: https://codex.buddypress.org/plugindev/post-types-activities/#adding-the-buddypress-support-and-specific-labels-at-post-type-registration


    Monkey1980
    Participant

    @monkey1980

    1. I just did it to test, the original code you provided was for pages – having established that it worked I then had an entry in the activity, in the admin it showed ‘Published a new page’ – i then changed the post type to my custom one and set my action id and notice in the admin it said ‘Unregistered action – new_blog_page’ in the Action Column for the page test entry – I realised this was the ‘action_id’ from your original code so new the code worked. It just does not work when I set my CPT for some reason. The CPT does create an activity entry but using the default message.

    2. It is registered using Easy Custom Post Types plugin, however I have another CPT in my child theme (services-type.php) and the same thing happens for this post type.

    3. This is possible for the CPT in my child theme but not the one’s created using the ECPT plugin – I will test with this disabled and perhaps migrate the CPT’s into the child theme if I find that it is causing the conflict.

    At What priority does Easy Custom Post Types hooks ‘init’ ?

    You need to be sure the post type has been defined before using bp_activity_set_post_type_tracking_args() so change ‘bp_init’ to ‘init’ and try to add a very late priority, eg:

    // Don't forget to add the 'buddypress-activity' support!
    add_post_type_support( 'sd_article', '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( 'sd_article', array(
            'component_id'             => 'activity',
            'action_id'                => 'new_sd_article',
            'bp_activity_admin_filter' => __( 'Neuer Artikel veröffentlicht', 'custom-domain' ),
            'bp_activity_front_filter' => __( 'Artikel', 'custom-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'bp_activity_new_post'     => __( '%1$s hat den neuen <a href="%2$s">Artikel</a>', 'sozialdynamik' ),
            'bp_activity_new_post_ms'  => __( '%1$s hat den neuen <a href="%2$s">Artikel</a>, on the site %3$s', 'sozialdynamik' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'init', 'customize_page_tracking_args', 1000 );

    Monkey1980
    Participant

    @monkey1980

    OMG – you are AMAZING!!! – that worked a treat, i’m so happy :0))

    Now, if only it could show the Article Title instead of ‘Artikel’ – that would be brilliant!

    Thanks you so much for your help here.

    This can be done using a specific filter. I’ll look at it soon. Glad the first step is achieved 🙂


    youmin
    Participant

    @youmin

    Isn’t it possible to register CPT and display it on group home page instead of activity or on both? Since posting an CPT article on group page is it self an activity. So isn’t on group home page as well ?

    What do I need to change in above piece of code ?


    Monkey1980
    Participant

    @monkey1980

    Thanks @imath – I really appreciate it …and so will my client!


    @youmin
    – I’m not sure i’m clear on what you ask but:

    ‘contexts’ => array( ‘activity’, ‘member’ ),

    I would assume ‘group’ (…or ‘groups’) would do the job? …i’m sure @imath will confirm bur seems reasonable to me (I don’t use groups so can’t confirm)


    youmin
    Participant

    @youmin

    Thanks Monkey1980 I’ll try, but I suspect it need more codes since in group you need to add the CPT as a menu in group admin menu. So just wanted to know injecting CPT (bp 2.2 method) in group home page.

    And I think we need to change this part

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

    To

    if ( ! bp_is_active( ‘group’ ) ) {
    return;
    }

    As well, ya imath will solve it , hoping soon

    Interesting discussion 🙂

    First i’ll explain why i’m using bp_is_active( 'activity' ) : it’s just an extra check to be absolutely sure the activity component is active. I do it because BuddyPress is a set of components you can activate / deactivate from the BuddyPress settings. So if the activity component is not active, no need to add Post Type activities. And when you develop a BuddyPress plugin, you need to remember there can be configs that deactivated the component you are extending.

    Second, about the contexts argument. In this codex page https://codex.buddypress.org/plugindev/post-types-activities/ i’m describing a bit his role
    Here https://codex.buddypress.org/themes/activity-dropdown-filters-in-templates/ i’m talking about the way we are getting the activity types since BuddyPress 2.1 And finally reading this page https://codex.buddypress.org/developer/function-examples/bp_activity_set_action/ you will see the interest of the contexts argument.
    In short the contexts argument is a way to control where you want an option (activity action) to be displayed. For instance if you want to include an option into the single group’s home page dropdown filter you do contexts => array( 'group' )

    In the case of a post type, i think in most cases there’s not a great interest to have the activity filter in the group’s single home page because in most cases the post type have been created out of a BuddyPress group (mainly in WP Admin actually). That’s why in my example i suggest to use 'contexts' => array( 'activity', 'member' ), ‘activity’ means display the dropdown option into the activity directory and ‘member’ display it into the member’s profile. The ‘member’ context is interesting in the case of Post Types activities because a post type is always created by a user_id.

    Now with the question “could it be possible to display Post type activities into a given group’s stream ?” we must understand this part 'component_id' => 'activity' Here you are defining the component you want the Post type activities to be linked to. So you could say cool! lets put ‘groups’ to have activities in groups. But here you’d be wrong because all generated activities would be displayed in the first group the one having the id #1 (If multisite it could give another illusion…) Why id #1, because it’s the id of the blog where was created the post type. So you would need to have a different value depending from which group the post type was posted. As in core we don’t include a feature to post a post type from a group, by default we are setting the item_id field to the current blog.

    Unless you want to arbitrary display the activity in one particular group, i’d say this need should only concern Plugins extending the Groups component. Of course it’s possible, and i’ve done it for one of my plugin.

    I hope my explanations will help you @youmin.

    @monkey1980, this is the missing step.

    Here’s how you can add the post type title into the link in replacement of ‘Artikel’

    function monkey1980_include_post_type_title( $action, $activity ) {
    	if ( empty( $activity->id ) ) {
    		return $action;
    	}
    
    	if ( 'new_sd_article' != $activity->type ) {
    		return $action;
    	}
    
    	preg_match_all( '/<a.*?>([^>]*)<\/a>/', $activity->action, $matches );
    
    	if ( empty( $matches[1][1] ) || 'Artikel' != $matches[1][1] ) {
    		return $action;
    	}
    
    	$post_type_title = bp_activity_get_meta( $activity->id, 'post_title' );
    
    	if ( empty( $post_type_title ) ) {
    		switch_to_blog( $activity->item_id );
    
    		$post_type_title = get_post_field( 'post_title', $activity->secondary_item_id );
    
    		// We have a title save it in activity meta to avoid switching blogs too much
    		if ( ! empty( $post_type_title ) ) {
    			bp_activity_update_meta( $activity->id, 'post_title', $post_type_title );
    		}
    
    		restore_current_blog();
    	}
    
    	return str_replace( $matches[1][1], esc_html( $post_type_title ), $activity->action );
    }
    add_filter( 'bp_activity_custom_post_type_post_action', 'monkey1980_include_post_type_title', 10, 2 );

    youmin
    Participant

    @youmin

    Thanks imath, that’s awesome. But this is only one part of my question and another part is Displaying CPT on group home page instead of activity .
    Example : like most plugins do injecting CPT by making sub nav menu ( may be by registering as a new component or with a slug ) or on both activity and group.


    Monkey1980
    Participant

    @monkey1980

    @imath …das ist perfekt!

    Though I tweaked it a little so as to have the correct German wording:

    if ( empty( $matches[1][1] ) || ‘link’ != $matches[1][1] ) {
    return $action;
    }

    —>

    leaving it as Artikel meant that changing the wording (below) resulted in the word ‘Artikel’ not displaying! lol – All good now though – you are wonderful sir! – Thanks for the BuddyPress update and your assistance here, have a great weekend. :0))

    —>

    ‘bp_activity_new_post’ => __( ‘%1$s hat den neuen Artikel link geschrieben’, ‘sozialdynamik’ ),
    ‘bp_activity_new_post_ms’ => __( ‘%1$s hat den neuen Artikel link geschrieben, on the site %3$s’, ‘sozialdynamik’ ),


    aaronkine
    Participant

    @aaronkin

    @imath thanks for the above code which “adds the post type title into the link in replacement”

    But i need to change one small thing in that code but i dont know how.

    Here is my code in bp-custom.php —

    add_post_type_support( 'job_listing', 'buddypress-activity' );
    function customize_page_tracking_args_job() {
        // Check if the Activity component is active before using it.
        if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
     
        bp_activity_set_post_type_tracking_args( 'job_listing', array(
    			'component_id' => 'activity',
            'action_id'                => 'new_job_listing',
            'bp_activity_admin_filter' => __( 'Published a new Job', 'text-domain' ),
            'bp_activity_front_filter' => __( 'Job', 'text-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s posted a new Job: <a href="%2$s">Job</a>', 'text-domain' ),
            'bp_activity_new_post_ms'  => __( '%1$s posted a new Job: <a href="%2$s">Job</a>, on the site %3$s', 'text-domain' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'init', 'customize_page_tracking_args_job', 999 );
    

    Here is the result…
    PathPoint-SSB posted a new Job: Job 5 hours, 53 minutes ago

    Here is what it looks like after I use your code to insert the Title of the Job
    pathpoint posted a new Receptionist/Administrative Assistant 5 hours, 49 minutes ago

    As you can see, it changes the Users name and also I cant change this part “posted a new”. I would like it to read “posted a new Job: ”

    Any ideas?

    Thanks for any insight on this,
    Aaron


    Mathieu Viet
    Moderator

    @imath

    I’ll look at it soon, but in the meantime is there any reason why you are not using the buddypress-activity support/custom labels when registering the post type. As far as i can remember this code was shared because the post type was registered by another plugin. Is it the case of your “job” post type ?


    aaronkine
    Participant

    @aaronkin

    Yes… the post type is registered by another plugin “Wp Job Manager”. I am new at coding so if i find a script and it actually works “i use it” even though there may be a more correct way.

    I also have another plugin “The Events Calendar” that has post type of ‘tribe_events’ and i am trying to do the same thing as the Job post type.

    I also don’t understand the difference between using “text-domain” and “custom-domain” for the bp_activity_admin_filter ?

    I used info from this Topic/Discussion here: https://buddypress.org/support/topic/cant-seem-to-get-bp-2-2s-post-types-activities-working/

    Here is my full code in bp-custom for Jobs and Events post types…

    //The 'WP Job Manager' post type
    add_post_type_support( 'job_listing', 'buddypress-activity' );
    
    function customize_page_tracking_args_job() {
        // Check if the Activity component is active before using it.
        if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
     
        bp_activity_set_post_type_tracking_args( 'job_listing', array(
    			'component_id' => 'activity',
            'action_id'                => 'new_job_listing',
            'bp_activity_admin_filter' => __( 'Published a new Job', 'text-domain' ),
            'bp_activity_front_filter' => __( 'Job', 'text-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s posted a new Job: <a href="%2$s">Job</a>', 'text-domain' ),
            'bp_activity_new_post_ms'  => __( '%1$s posted a new Job: <a href="%2$s">Job</a>, on the site %3$s', 'text-domain' ),
            'position'                 => 100,
        ) );
    }
    //'The Calednar Events' post type 
    add_action( 'init', 'customize_page_tracking_args_job', 999 );
    
    add_post_type_support( 'tribe_events', 'buddypress-activity' );
    
    function customize_page_tracking_args_event() {
        // Check if the Activity component is active before using it.
        if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
     
        bp_activity_set_post_type_tracking_args( 'tribe_events', array(
    			'component_id' => 'activity',
            'action_id'                => 'new_tribe_events',
            'bp_activity_admin_filter' => __( 'Published a new Event', 'text-domain' ),
            'bp_activity_front_filter' => __( 'Event', 'custom-domain' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s posted an Event: <a href="%2$s">Event</a>', 'custom-domain' ),
            'bp_activity_new_post_ms'  => __( '%1$s posted an Event: <a href="%2$s">Event</a>, on the site %3$s', 'custom-domain' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'init', 'customize_page_tracking_args_event', 999 );
    

    Mathieu Viet
    Moderator

    @imath

    @aaronkin

    First ‘text-domain’ or ‘custom-domain’. When you build a plugin, to make it an “international” plugin so that it can be translated in any language, you choose a text domain. See: https://codex.wordpress.org/I18n_for_WordPress_Developers#Text_Domains

    Then Using post type activities. You were actually missing one of the functions i’ve shared in this thread. So here’s a gist that will work for the WP Job Manager plugin, you’ll have to observe, learn some WordPress/BuddyPress developing stuff (such as the Plugin API) by yourself to understand it and be able to apply for any post types.


    aaronkine
    Participant

    @aaronkin

    Finally have time to reply….

    THANKS!! for taking the time to help me. Very grateful.

    so simple that all that was needed is the brackets [Job].


    mvp13585
    Participant

    @mvp13585

    Hi,

    i have similar problem.
    i have a custom post type “dwqa-question”

    i try the above code but the activity use default output.

    here my code in bp-custom :

    //dwqa-question
    
    function dwqa_question_activity_args() {
    	if ( ! bp_is_active( 'activity' ) ) {
    		return;
    	}
    
    	add_post_type_support( 'dwqa-question', 'buddypress-activity' );
    
    	bp_activity_set_post_type_tracking_args( 'dwqa-question', array(
    		'component_id'             => 'activity',
    		'action_id'                => 'new_dwqa_question',
            	'bp_activity_admin_filter' => __( 'Published a new Question', 'text-domain' ),
            	'bp_activity_front_filter' => __( 'Question', 'text-domain' ),
            	'contexts'                 => array( 'activity', 'member' ),
            	'activity_comment'         => true,
            	'bp_activity_new_post'     => __( '%1$s posted a new Question: <a href="%2$s">[Question]</a>', 'text-domain' ),
            	'bp_activity_new_post_ms'  => __( '%1$s posted a new Question: <a href="%2$s">[Question]</a>, on the site %3$s', 'text-domain' ),
            	'position'                 => 101,
        	) );
    }
    add_action( 'init', 'dwqa_question_activity_args' );
    
    function dwqa_question_include_post_type_title( $action, $activity ) {
    	if ( empty( $activity->id ) ) {
    		return $action;
    	}
    
    	if ( 'new_dwqa_question' != $activity->type ) {
    		return $action;
    	}
    
    	preg_match_all( '/<a.*?>([^>]*)<\/a>/', $action, $matches );
    
    	if ( empty( $matches[1][1] ) || '[Question]' != $matches[1][1] ) {
    		return $action;
    	}
    
    	$post_type_title = bp_activity_get_meta( $activity->id, 'post_title' );
    
    	if ( empty( $post_type_title ) ) {
    
    		switch_to_blog( $activity->item_id );
    
    		$post_type_title = get_post_field( 'post_title', $activity->secondary_item_id );
    
    		// We have a title save it in activity meta to avoid switching blogs too much
    		if ( ! empty( $post_type_title ) ) {
    			bp_activity_update_meta( $activity->id, 'post_title', $post_type_title );
    		}
    
    		restore_current_blog();
    	}
    
    	return str_replace( $matches[1][1], esc_html( $post_type_title ), $action );
    }
    add_filter( 'bp_activity_custom_post_type_post_action', 'dwqa_question_include_post_type_title', 10, 2 );

    do you know what’s wrong with this code?


    milenushka
    Participant

    @milenushka

    Hi @imath

    I am sort of loosing my hair since I updated to the latest version of buddypress.
    had searched the forum a few months back and put a LOT of time into custom coding that the activity will read my custom post types. I put that code in my functions.php file and was really happy with everything- each activity had it’s own text and it was really nice.

    I am not a coder, just building my site, and now, since this update, all my work is gone- all I get is the generic “posted a new item”, and a link to the post- not even the name of the post. I have 3 custom post types, and I need them all to be unique.
    I don’t know what to do now, how to start over?

    Can I just cancel the new custom post support and go back to my own code?
    Please help.

    Hi @milenushka,

    Using your old code means using the old filters, i guess. If you do so right now, it will not be ok, 2.3 will include a fix to make this works. But you must know these filters will be deprecated, so it might Be a good idea to use the new way of registering activity support for your post types. Could you share the code you were using and i’ll share with you the code that Will mimic your old code using the new way of registering activity support.


    milenushka
    Participant

    @milenushka

    Hi @imath, thank you so much for writing back. For some reason I didn’t get the notification, and was depressed over the site for a month. Today I decided to finally face the issue again, and what a nice surprise!

    So my code is:

    add_filter( 'bp_blogs_record_post_post_types', 'inspired_record_more_types' );
    function inspired_record_more_types( $types ) {
         $types[] = 'projects';
         $types[] = 'sfwd-courses';
         $types[] = 'uig_image';
            return $types;
    }
    
    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 == 'projects' ) {
    	$activity_action  = sprintf( __( '%1$s created a new project, %2$s  ', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
       }
    if( $post->post_type == 'sfwd-courses' ) {
    	$activity_action  = sprintf( __( '%1$s added a new workshop, %2$s  ', 'buddypress'  ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
       }	
    if( $post->post_type == 'uig_image' ) {
    	$activity_action  = sprintf( __( '%1$s added a new image, %2$s  ', 'buddypress'  ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
       }
       return $activity_action;
    }
    
    function bbg_record_my_custom_post_type_comments( $post_types ) {
          $post_types[] = 'projects'; 
          return $post_types;
      }
      add_filter( 'bp_blogs_record_comment_post_types', 'bbg_record_my_custom_post_type_comments' );
    
    

    Thank you so much. I really appreciate it.

    Hi @milenushka,

    I see you are also filtering bp_blogs_record_comment_post_types to track comments about the “projects” post type. I’ve changed my mind 🙂 I think we should wait to have this ticket fixed first.

    Since 2.3 your code should work fine.


    clueson
    Participant

    @clueson

Viewing 25 replies - 1 through 25 (of 36 total)
  • You must be logged in to reply to this topic.
Skip to toolbar