Custom Posts in Activity Stream.
-
Try this (the second function adds the course title):
function customize_page_tracking_args() { if ( ! bp_is_active( 'activity' ) ) { return; } add_post_type_support( 'sfwd-courses', 'buddypress-activity' ); bp_activity_set_post_type_tracking_args( 'sfwd-courses', array( 'action_id' => 'new_sfwd-courses', 'bp_activity_admin_filter' => __( 'Published a new course', 'custom-textdomain' ), 'bp_activity_front_filter' => __( 'Course', 'custom-textdomain' ), 'bp_activity_new_post' => __( '%1$s wrote a new course, <a href="%2$s">[Course]</a>', 'custom-textdomain' ), 'bp_activity_new_post_ms' => __( '%1$s wrote a new course, <a href="%2$s">[Course]</a>, on the site %3$s', 'custom-textdomain' ), 'contexts' => array( 'activity', 'member' ), 'comment_action_id' => 'new_sfwd-courses_comment', 'bp_activity_comments_admin_filter' => __( 'Commented on a course', 'custom-textdomain' ), 'bp_activity_comments_front_filter' => __( 'Course Comments', 'custom-textdomain' ), 'bp_activity_new_comment' => __( '%1$s commented on a <a href="%2$s">Course</a>', 'custom-textdomain' ), 'bp_activity_new_comment_ms' => __( '%1$s commented on a <a href="%2$s">Course</a>, on the site %3$s', 'custom-textdomain' ), 'position' => 100, ) ); } add_action( 'init', 'customize_page_tracking_args', 1000 ); function new_sfwd-courses_include_post_type_title( $action, $activity ) { if ( empty( $activity->id ) ) { return $action; } if ( 'new_sfwd-courses' != $activity->type ) { return $action; } preg_match_all( '/<a.*?>([^>]*)<\/a>/', $action, $matches ); if ( empty( $matches[1][1] ) || '[Course]' != $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', 'new_sfwd-courses_include_post_type_title', 10, 2 );
Hi, thank you for replying however when I used the code you provided in my bp-custom.php my site began to give error 500 messages.
The second function, that changes the name to the name of the content seems to be the problem because once I took that out the site loaded fine. Do you have any idea why that may have been the problem?
Try this. It may be the dash in the function name (I edited it in a hurry):
function new_sfwd_courses_include_post_type_title( $action, $activity ) { if ( empty( $activity->id ) ) { return $action; } if ( 'new_sfwd-courses' != $activity->type ) { return $action; } preg_match_all( '/<a.*?>([^>]*)<\/a>/', $action, $matches ); if ( empty( $matches[1][1] ) || '[Course]' != $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', 'new_sfwd_courses_include_post_type_title', 10, 2 );
That’s fantastic! It works, now the name of the course that I have created is showing and the users can comment on it from the activity stream.
One question I have though is, I’m not sure if this was meant to happen but when a user comments on the course page the comment is embedded in as a reply in the activity stream. Example
Do you know if there is a way of getting the comment to be posted to the top of the activity stream? My activity stream is already set up to sort the content from the most recent at the top, but it would be much better if the comment could be placed at the top because it could maybe be lost since it’s sticking with when course was created.
Hi, this is just what i needed, thanks, but I have several custom post types. I tried replicating the code, but received the error:
Cannot redeclare customize_page_tracking_args()
Could you help please?Oops! sorted by renaming so there were no duplicates
- You must be logged in to reply to this topic.
anthlon
@anthlon
8 years, 3 months ago
Hi, I’m using Buddypress (2.6.1.1) and LearnDash (2.2.1.1) and the BuddyPress for LearnDash add-on (1.1.0.0) with WordPress (4.5.3) to create a website that allows users to learn more about certain subjects and also gives them a place where they can speak about the content that’s on the website, for this I decided to use BuddyPress since it’s Activity Feed and use of Groups seemed like a very good fit for what I was looking for.
I’m having a bit of trouble getting comments made on the LearnDash Courses appearing in the BuddyPress Activity feed, I was following this:
Post Types Activities Page
When I add:
add_post_type_support( 'sfwd-courses', 'buddypress-activity' );
to bp-custom.php the creation of a new course is posted to the activity feed, however you can’t comment on it from the activity feed and comments on the Course Page don’t show on the Activity Feed.
Adding the function Customize_page_tracking_args() to my bp-custom.php also doesn’t change anything and I’m still told that “Admin has added a new item”
Activity Feed Post
Pastebin of bp-custom.php