Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add an activity item for a new custom post type item creation.
<?php
/**
* Create an activity item to appear in a group. Fires once a post has been saved.
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function add_activity_item_for_group( $post_id, $post_object, $update ) {
if ( $update ) {
return;
}
$bp = buddypress();
$author_id = (int) $post_object->post_author;
$user_link = bp_core_get_userlink( $author_id );
$post_type_object = get_post_type_object( $post_object->post_type );
$post_type_label = strtolower( $post_type_object->labels->singular_name );
$group_id = 0; // Whatever your logic is here to find the right group ID.
$type = $post_object->post_type . '_created';
// Does an activity item already exist?
$exists = bp_activity_get_activity_id( array(
'component' => $bp->groups->id,
'type' => $type,
'item_id' => $group_id,
'secondary_item_id' => $post_id,
) );
if ( ! empty( $exists ) ) {
continue;
}
// Create a post url that is relative to this group.
$post_url = trailingslashit( ccgn_get_home_permalink( $group_id ) ) . $post_object->post_name;
$post_link = sprintf( '<a href="%s">%s</a>', $post_url, $post_object->post_title );
$group = groups_get_group( array( 'group_id' => $group_id ) );
$group_url = bp_get_group_permalink( $group );
$group_link = '<a href="' . $group_url . '">' . $group->name . '</a>';
// Only set hide_sitewide to false if this is a public group.
if ( 'public' == bp_get_group_status( $group ) ) {
$hide_sitewide = false;
} else {
$hide_sitewide = true;
}
$action = sprintf( __( '%1$s published the %2$s %3$s in the group %4$s', 'language_string' ), $user_link, $post_type_label, $post_link, $group_link );
$args = array(
'user_id' => $author_id,
'action' => $action,
'primary_link' => $post_link,
'component' => $bp->groups->id,
'type' => $type,
'item_id' => $group_id, // Set to the group id
'secondary_item_id' => $post_id, // The id of the post itself
'recorded_time' => $post_object->post_date,
'hide_sitewide' => $hide_sitewide,
'content' => bp_create_excerpt( $post_object->post_content, 358 )
);
do_action( $post_object->post_type . '_before_activity_save', $args );
$activity_id = bp_activity_add( apply_filters( $post_object->post_type . '_activity_args', $args, $post_id ) );
}
add_action( "save_post_{$post_type}", 'add_activity_item_for_group' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.