BBP_BuddyPress_Activity::topic_create( int $topic_id, int $forum_id, array $anonymous_data = array(), int $topic_author_id )
Record an activity stream entry when a topic is created or updated
Description
Parameters
- $topic_id
-
(Required)
- $forum_id
-
(Required)
- $anonymous_data
-
(Optional)
Default value: array()
- $topic_author_id
-
(Required)
Return
(Bail) early if topic is by anonymous user
Source
File: bp-forums/activity.php
public function topic_create( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $topic_author_id = 0 ) {
// Bail early if topic is by anonymous user
if ( !empty( $anonymous_data ) )
return;
// Validate activity data
$user_id = (int) $topic_author_id;
$topic_id = bbp_get_topic_id( $topic_id );
$forum_id = bbp_get_forum_id( $forum_id );
// Bail if user is not active
if ( bbp_is_user_inactive( $user_id ) )
return;
// Bail if topic is not published
if ( !bbp_is_topic_published( $topic_id ) )
return;
// User link for topic author
$user_link = bbp_get_user_profile_link( $user_id );
// Topic
$topic_permalink = bbp_get_topic_permalink( $topic_id );
$topic_title = get_post_field( 'post_title', $topic_id, 'raw' );
$topic_content = get_post_field( 'post_content', $topic_id, 'raw' );
$topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
// Forum
$forum_permalink = bbp_get_forum_permalink( $forum_id );
$forum_title = get_post_field( 'post_title', $forum_id, 'raw' );
$forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
// Activity action & text
$activity_text = sprintf( esc_html__( '%1$s started the discussion %2$s in the forum %3$s', 'buddyboss' ), $user_link, $topic_link, $forum_link );
$activity_action = apply_filters( 'bbp_activity_topic_create', $activity_text, $user_id, $topic_id, $forum_id );
$activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', $topic_content );
// Compile and record the activity stream results
$activity_id = $this->record_activity( array(
'id' => $this->get_activity_id( $topic_id ),
'user_id' => $user_id,
'action' => $activity_action,
'content' => $activity_content,
'primary_link' => $topic_permalink,
'type' => $this->topic_create,
'item_id' => $topic_id,
'secondary_item_id' => $forum_id,
'recorded_time' => get_post_time( 'Y-m-d H:i:s', true, $topic_id ),
'hide_sitewide' => ! bbp_is_forum_public( $forum_id, false )
) );
// Add the activity entry ID as a meta value to the topic
if ( !empty( $activity_id ) ) {
update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
}
}
Changelog
| Version | Description |
|---|---|
| bbPress (r3395) | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.