Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Buddypress custom post type activity WordPress

I have a Gravity Form allowing users to submit to a Custom Post Type. During the post submission, the user is able to select a related group - the idea being that the user's post shows in the group's activity stream.

This only seems to work when the user is an Administrator. If the user is any other role, regardless of whether they're a member of the group or not, when they submit the post (which is automatically published), it doesn't show in the group's activity stream.

Here's the code for the BuddyPress activity with the custom post type:


<?php


//add_filter( 'bp_blogs_record_post_post_types', 'activity_publish_custom_post_types', 1 );
//add_action( 'publish_rewards', 'groups_reward_activity', 99, 1 );
//add_action( 'gform_after_submission_2', 'groups_reward_activity', 10, 3);

add_filter('bp_blogs_record_post_post_types', 'activity_publish_custom_post_types',1);
add_filter('bp_blogs_activity_new_post_action', 'rewards_activity_action', 1, 3);
add_action('publish_reward', 'groups_reward_activity',99,1);
add_action('gform_after_submission_2', 'groups_reward_activity',10,3);

add_action( 'before_delete_post', 'reward_delete_action' );


/* Tell Buddypress to add Rewards to activity stream */
function activity_publish_custom_post_types( $post_types ) {

$post_types[] = 'reward';
return $post_types;
}

/* Filter for action for post type reward*/
function rewards_activity_action($activity_action, $post, $post_permalink)
{
global $bp;
if ($post->post_type == 'reward') {
$activity_action = sprintf(__('%1$s posted a new reward : %2$s', 'buddypress'), bp_core_get_userlink((int) $post->post_author), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>');
return $activity_action;
}
}

function groups_reward_activity( $post_id, $post = null, $user_id = false ) {
global $bp, $post;

// echo '<pre>';
// print_r($post_id);

// TF - Added function to pull post from database if not present.
if ($post == null)
{
$post = get_post($post_id); // This was not originally passed as an array!
$post_id = $post->ID;
}

//print_r(func_get_args()); die;
// print_r($post); die;

if ( $post->post_type != 'reward' ) return false;

if ( !$user_id ) $user_id = (int)$post->post_author;

$post_type = get_post_type( $post_id );
$thumbnail = get_the_post_thumbnail($post_id, '', array( 'class' => 'scale-with-grid' ) );
$post_permalink = get_permalink($post_id);
$title = get_the_title($post_id);

if($_POST["fields"]){
$fields = $_POST["fields"]; //$_POST contains all of the submitted data

if($fields){
$field_price_percentage = $fields['field_54e21a8894a43'];
$field_short_description = $fields['field_54e21ac494a44'];
$field_offer_type = $fields['field_54e224c5eacdb'];
$cat = $fields['field_54e225b2eacde'];
$field_group = $fields = $fields['field_54f9a6b5ab748'];// field_54e22533eacdd
}
} else {
// TF - Pull data from meta if not in fields.
// If there are no posted fields then we must extract the data from the post meta
$meta = get_post_meta($post_id);
$field_price_percentage = $meta['price_percentage'][0];
$field_short_description = $meta['short_description'][0];
$field_offer_type = $meta['offer_type'][0];
$cat = $meta['category'][0];
$field_group = $meta['locations'];
//print_r($field_group); die;
}
// print_r($fields);
// print_r($meta);

switch ( $cat ) {
case 'drink':
$background_color = '#e33d36';
break;

case 'eat':
$background_color = '#17d1a1';
break;

case 'event':
$background_color = '#c03ec6';
break;

case 'fashion':
$background_color = '#93cc22';
break;

case 'pop-up':
$background_color = '#4663ca';
break;

case 'shop':
$background_color = '#f2ba2b';
break;

case 'activity':
$background_color = '#47c2c8';
break;

case 'music':
$background_color = '#e85e8c';
break;

case 'beauty':
$background_color = '#f4005d';
break;

case 'festivals':
$background_color = '#e99c00';
break;
}

switch ( $field_offer_type ) {
case 'pp' :
$field_offer = ' PP';
break;
case 'off' :
$field_offer = ' OFF';
break;
default :
$field_offer = '';
break;
}

$activity_content = "<div class='reward fourteen columns alpha omega'>";
$activity_content .= "<div class='reward-bg two-thirds column alpha omega'>{$thumbnail}</div>";
$activity_content .= "<div class='circle one-third column alpha omega {$cat}'>";
$activity_content .= "<div class='reward-details'><span class='h2'>{$field_price_percentage} ";
$activity_content .= "<span>{$field_offer}</span>";
$activity_content .= "</span>";
$activity_content .= "<span class='h4'>{$field_short_description}</span></div>";
$activity_content .= "</div>";
$activity_content .= "<div class='claim'>";
$activity_content .= "<a href='{$post_permalink}'><h6>Claim it >></h6></a>";
$activity_content .= "</div>";
$activity_content .= "</div>";

$max = count($field_group);
for ($i = 0; $i < $max; $i++) {
$group_id = (int) BP_Groups_Group::group_exists($field_group[$i]);

// print_r($group_id);

$bp->groups->current_group = groups_get_group( array( 'group_id' => $group_id ) );

$groups_activity_action = sprintf( __( '%1$s posted a reward %2$s in the circle %3$s', 'buddypress'), bp_core_get_userlink( $user_id ),'<a href="' . $post_permalink . '">' . $post->post_title . '</a>' , '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );


$groups_args = array(
'action' => apply_filters( 'groups_activity_new_update_action', $groups_activity_action ),
'content' => apply_filters( 'groups_activity_new_update_content', $activity_content ),
'type' => 'activity_update',
'primary_link' => $post_permalink,
'secondary_item_id' => $post_id,
'item_id' => $group_id,
'user_id' => $user_id,
'component' => 'groups',
); //all of groups_args array value is being populated

// print_r($groups_args); die;

// $res = bp_activity_delete( array( 'user_id' => $user_id, 'secondary_item_id' => $post_id) );
$res = BP_Activity_Activity::delete( array( 'user_id' => $user_id, 'secondary_item_id'=> $post_id, 'type' => 'activity_update', 'item_id' => $group_id, 'component' => 'groups') );
// echo $res?'yep':'no';
// echo '-----';

groups_record_activity($groups_args);

wp_cache_delete( 'groups_group_' . $group_id );
}
}

// Deletes rewards from the circles
function reward_delete_action($post_id)
{
global $post_type;

if ( $post_type != 'reward' ) return;

$post = get_post($post_id);

if ( !$user_id ) $user_id = (int)$post->post_author;

$meta = get_post_meta($post_id);

$field_group = $meta['locations'];

$res = BP_Activity_Activity::delete( array( 'user_id' => $user_id, 'secondary_item_id'=> $post_id, 'type' => 'activity_update', 'item_id' => $group_id, 'component' => 'groups') );


}

Answers (2)

2015-03-10

Arnav Joy answers:

Hi Dan,

can you show me your site ?

2015-03-11

Andrea P answers:

it should be already set to false by default, but you could try declaring this in the group_args:

'hide_sitewide' => false

also, you might try to set the type (in the same group_args array) to be one of those rather than 'activity update'

'type' => 'new_forum_topic'
'type' => 'new_forum_post'

I'm just trying to guess, because the code looks ok and in fact the activity is actually printed in the stream, but it's set to be visibile only to admins (so could be private?). or maybe it's visible only by the author of the post? have you tried posting with a normal user and then check the stream with that account?