文章类型活动

文章类型活动

Codex Home → BuddyPress Plugin Development → Post Types Activities
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 )
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.
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:
add_post_type_support( 'page', 'buddypress-activity' );

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 item', '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()
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:
// 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 page', 'custom-textdomain' ),
'bp_activity_new_post_ms' => __( '%1$s posted a new page, on the site %3$s', 'custom-textdomain' ),
'position' => 100,
) );
}
add_action( 'bp_init', 'customize_page_tracking_args' );

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 item', '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 item, on the site %3$s', 'buddypress' )
$position
An integer to set the order of the action within the component』s actions

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.
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 book', 'your-plugin-textdomain' ),
'bp_activity_new_post_ms' => __( '%1$s posted a new book, 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' );

Important note: public parameter should be defined and set to true, otherwise the connection between post type and Activity component won』t be established.
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.
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');

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 item', '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 item, on the site %3$s', 'buddypress' )

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.
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 page', 'custom-textdomain' ),
'bp_activity_new_post_ms' => __( '%1$s posted a new page, 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 page', 'custom-textdomain' ),
'bp_activity_new_comment_ms' => __( '%1$s commented on the page, on the site %3$s', 'custom-textdomain' ),
'position' => 100,
) );
}
add_action( 'bp_init', 'customize_page_tracking_args' );

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.
$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 foo', 'custom-textdomain' ),
'bp_activity_new_comment_ms' => __( '%1$s commented on the foo, 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
),
) );

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.

Related Resources

register_post_type() WordPress Codex page
Posting activities from plugins
Activity dropdown filters in templates
More details and explanation in this topic
More details and possible field values on bp_activity_set_action() page

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注