Post Types Activities

Note: This guide is for use with BuddyPress 2.2+.

Any public registered post type can be tracked into the activity streams once it has been published. Here are the different options to add the “buddypress-activity” support to a public post type.

Activate the Blogs component ( aka Site Tracking ) Activate the Blogs component ( aka Site Tracking )

If you activate the Site Tracking component from the BuddyPress component’s Administration screen, the post post type will automatically have the “buddypress-activity” feature enable. Comments about this post type will also generate activities or, if you chose to “allow the activity stream commenting on blog posts” from the BuddyPress settings Administration screen, comments will be synchronized with the activity stream.

Top ↑

Use add_post_type_support( ‘post_type’, ‘buddypress-activity’ ) Use add_post_type_support( ‘post_type’, ‘buddypress-activity’ )

From your bp-custom.php file, you can also manually set a post type to be tracked into the activity stream. For instance, for the page post type you could use this code:

[sourcecode language=”php”]
add_post_type_support( ‘page’, ‘buddypress-activity’ );
[/sourcecode]

In this case, BuddyPress will set an activity action using generic activity attributes : the component, the generated activities will be attached to, will be the Activity component; the action type will be the post type’s name prefixed by the mention ‘new_’ (in our example ‘new_page’); the label of the front-end activity dropdown filters will be the name parameter of the ‘labels’ argument of the post type (in our example ‘Pages’); the label of the Activity Administration screens dropdown filters will be __( 'New item published', 'buddypress' ); the action string will be __( '%1$s wrote a new <a href="%2$s">item</a>', 'buddypress' ) where %1$s will be replaced by the member’s link of the post type’s author and %2$s by the url to the post type; the context will be limited to the Activity directory; commenting on generated activities about the post type will be possible if the post type is not supporting comments.

Simply by using add_post_type_support()

Simply by using add_post_type_support()

Top ↑

Customizing generic activity attributes Customizing generic activity attributes

You can edit the generic activity attributes of a post type from your bp-custom.php file using the function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array() ). Use the first parameter to inform about the post type you wish to edit the activity attributes of. The second parameter is an associative array containing the activity attributes to edit. For instance you could use this code:

[sourcecode language=”php”]
// 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’ );
[/sourcecode]

Using add_post_type_support() and customizing activity attributes.

Using add_post_type_support() and customizing activity attributes.

Below a description of the parameters used above:

$component_id
The unique string ID of the component the activity action is attached to, defaults to ‘activity’
$action_id
A string that describes the action type and that is used in the front-end and in the Activity Administration screens as the value of the activity dropdown filters, defaults to ‘new_{post_type_name}’
$bp_activity_admin_filter
A string that describes the action description and that is used in the Activity Administration screens as the label of the activity dropdown filters.
$bp_activity_front_filter
A string that describes the action label of the activity front-end dropdown filters.
$contexts
An array that describes the Activity stream contexts where the filter should appear. Possible values are ‘activity’, ‘member’, ‘member_groups’, ‘group’.
$activity_comment
A boolean to allow/disallow comments on the activity items. Defaults to true if the post type does not natively support comments, otherwise false.
$bp_activity_new_post
A string containing the custom action string to use for regular configs. Defaults to __( '%1$s wrote a new <a href="%2$s">item</a>', 'buddypress' )
$bp_activity_new_post_ms
A string containing the custom action string to use for multisite configs. Defaults to __( '%1$s wrote a new <a href="%2$s">item</a>, on the site %3$s', 'buddypress' )
$position
An integer to set the order of the action within the component’s actions

Top ↑

Adding the “buddypress-support” and specific labels at post type registration Adding the “buddypress-support” and specific labels at post type registration

When registering a post type in WordPress it’s also possible to set the ‘buddypress-activity’ feature using the support parameter of the second argument of the register_post_type() function. Custom activity action strings can be defined within the labels parameter and activity attributes can be set using the parameter ‘bp_activity’. See below for an example of use.

[sourcecode language=”php”]
function plugin_registers_post_type() {
$args = array(
‘public’ => true,
‘labels’ => array(
‘name’ => __( ‘Books’, ‘your-plugin-textdomain’ ),
‘singular_name’ => __( ‘Book’, ‘your-plugin-textdomain’ ),
‘bp_activity_admin_filter’ => __( ‘New book published’, ‘your-plugin-textdomain’ ),
‘bp_activity_front_filter’ => __( ‘Books’, ‘your-plugin-textdomain’ ),
‘bp_activity_new_post’ => __( ‘%1$s posted a new <a href="%2$s">book</a>’, ‘your-plugin-textdomain’ ),
‘bp_activity_new_post_ms’ => __( ‘%1$s posted a new <a href="%2$s">book</a>, on the site %3$s’, ‘your-plugin-textdomain’ ),
),
‘supports’ => array( ‘title’, ‘editor’, ‘buddypress-activity’ ),
‘bp_activity’ => array(
‘component_id’ => buddypress()->activity->id,
‘action_id’ => ‘new_book’,
‘contexts’ => array( ‘activity’, ‘member’ ),
‘position’ => 40,
),
);
register_post_type( ‘book’, $args );
}
add_action( ‘init’, ‘plugin_registers_post_type’ );
[/sourcecode]

Important note: public parameter should be defined and set to true, otherwise the connection between post type and Activity component won’t be established.

Top ↑

Customizing Post Type Activity Content Customizing Post Type Activity Content

You can customize the activity action of a Post Type via the register_post_type() example above. If you want to customize the Post Type activity content you can filter bp_before_activity_add_parse_args or bp_after_activity_add_parse_args.

[sourcecode language=”php”]
function record_cpt_activity_content( $cpt ) {

if ( ‘new_book’ === $cpt[‘type’] ) {

$cpt[‘content’] = ‘what you need’;
}

return $cpt;
}
add_filter(‘bp_before_activity_add_parse_args’, ‘record_cpt_activity_content’);
[/sourcecode]

Top ↑

Tracking comments about a Post Type Tracking comments about a Post Type

Version 2.5 is bringing Post Type comments tracking into the Activity stream for the Post Types supporting the 'comments' and the 'buddypress-activity' features. You can set the comments tracking feature to an existing Post Type or during the Post Type registration by adding the 'comment_action_id' specific parameter. If you wish to customize dropdown filter labels and action strings, you simply need to define some extra parameters. Here is the list of the available parameters :

$comment_action_id
A string that describes the action type and that is used in the front-end and in the Activity Administration screens as the value of the activity dropdown filters
$bp_activity_comments_admin_filter
A string that describes the action description and that is used in the Activity Administration screens as the label of the activity dropdown filters.
$bp_activity_comments_front_filter
A string that describes the action label of the activity front-end dropdown filters.
$bp_activity_new_comment
A string containing the custom action string to use for regular configs. Defaults to __( '%1$s commented on the <a href="%2$s">item</a>', 'buddypress' )
$bp_activity_new_comment_ms
A string containing the custom action string to use for multisite configs. Defaults to __( '%1$s commented on the <a href="%2$s">item</a>, on the site %3$s', 'buddypress' )

1. Add comments tracking to an Existing Post Type 1. Add comments tracking to an Existing Post Type

Below is an example of code to put into your bp-custom.php file to add the comments tracking feature to the “page” Post Type.

[sourcecode language=”php”]
function customize_page_tracking_args() {
// Check if the Activity component is active before using it.
if ( ! bp_is_active( ‘activity’ ) ) {
return;
}

// Don’t forget to add the ‘buddypress-activity’ support!
add_post_type_support( ‘page’, ‘buddypress-activity’ );

/**
* Also don’t forget to allow comments from the WordPress Edit Page screen
* see this screencap https://cldup.com/nsl4TxBV_j.png
*/

bp_activity_set_post_type_tracking_args( ‘page’, array(
‘action_id’ => ‘new_blog_page’,
‘bp_activity_admin_filter’ => __( ‘Published a new page’, ‘custom-textdomain’ ),
‘bp_activity_front_filter’ => __( ‘Page’, ‘custom-textdomain’ ),
‘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’ ),
‘contexts’ => array( ‘activity’, ‘member’ ),
‘comment_action_id’ => ‘new_blog_page_comment’,
‘bp_activity_comments_admin_filter’ => __( ‘Commented a page’, ‘custom-textdomain’ ),
‘bp_activity_comments_front_filter’ => __( ‘Pages Comments’, ‘custom-textdomain’ ),
‘bp_activity_new_comment’ => __( ‘%1$s commented on the <a href="%2$s">page</a>’, ‘custom-textdomain’ ),
‘bp_activity_new_comment_ms’ => __( ‘%1$s commented on the <a href="%2$s">page</a>, on the site %3$s’, ‘custom-textdomain’ ),
‘position’ => 100,
) );
}
add_action( ‘bp_init’, ‘customize_page_tracking_args’ );
[/sourcecode]

Top ↑

2. Add comments tracking feature during the Post Type registration 2. Add comments tracking feature during the Post Type registration

Below is an example of code to put into your bp-custom.php file to registyer a “foo” Post Type and directly set the 'buddypress-activity' and 'comments' supports during this registration.

[sourcecode language=”php”]
$labels = array(
‘name’ => ‘foos’,
‘singular_name’ => ‘foo’,
‘bp_activity_comments_admin_filter’ => __( ‘Comments about foos’, ‘custom-textdomain’ ), // label for the Admin dropdown filter
‘bp_activity_comments_front_filter’ => __( ‘Foo Comments’, ‘custom-textdomain’ ), // label for the Front dropdown filter
‘bp_activity_new_comment’ => __( ‘%1$s commented on the <a href="%2$s">foo</a>’, ‘custom-textdomain’ ),
‘bp_activity_new_comment_ms’ => __( ‘%1$s commented on the <a href="%2$s">foo</a>, on the site %3$s’, ‘custom-textdomain’ )
);

register_post_type( ‘foo’, array(
‘labels’ => $labels,
‘public’ => true,
‘supports’ => array( ‘buddypress-activity’, ‘comments’ ), // Adding the comments support
‘bp_activity’ => array(
‘action_id’ => ‘new_foo’, // The activity type for posts
‘comment_action_id’ => ‘new_foo_comment’, // The activity type for comments
),
) );
[/sourcecode]

Top ↑

Notes Notes

On multisite configurations, the post types that are registered on any sub-site will need to be registered on the root site in order to be listed in the Activity dropdown filters. That’s the case for the post and page post types

Some of you may have used the 'bp_blogs_record_post_post_types' filter to include custom post types to the site tracking having the Blogs component activated. These post types will keep on being included as long as the Blogs component is active.

About Post Type comments tracking: if the Blogs component (Site Tracking) is active and if the “Allow activity stream commenting on blog and forum posts” setting is checked, comments about the Post Type will be synchronized with the activity comments about the corresponding Post Type activity.

Top ↑