Plugin Author
scribu
(@scribu)
First thing you need to understand is that there are two kinds of actions:
* registering a connection type (happens on each page load)
* creating a new connection of a certain type
For adding connections to the activity stream, you’d need to use the ‘p2p_created_connection’ hook:
function handle_new_connection( $p2p_id ) {
$connection = p2p_get_connection( $p2p_id );
if ( 'YOUR_CONNECTION_TYPE' == $connection->p2p_type ) {
bp_activity_add( ... );
}
}
add_action( 'p2p_created_connection', 'handle_new_connection' );
Ok cool, I’ll try it out.
I’m glad you were able to explain the ‘p2p_created_connection’ hook, I might never have figured that out. Thank you.
There does’t seem to be any documentation on the function bp_activity_add(). Do you have any idea how I would fill out these parameters or pass along the username and the title of the post that are now connected? These are some of the parameters I saw other people filling out in their use of the bp_activity_add().
function handle_new_connection( $p2p_id ) {
$connection = p2p_get_connection( $p2p_id );
global $bp;
if ( 'YOUR_CONNECTION_TYPE' == $connection->p2p_type ) {
bp_activity_add(array(
'action' => 'test',
'content' => 'Congratulations, you have just received a new badge!',
'component' => 'profile',
//'type' => '',
//'primary_link' => '',
'user_id' => $user_id,
));
}
}
add_action( 'p2p_created_connection', 'handle_new_connection' );
Hi,
I’m a little late for the party, but this is how I managed to add a new connection to the BP Stream:
function handle_new_connection( $p2p_id ) {
$connection = p2p_get_connection( $p2p_id );
if ( 'tweets_to_posts' == $connection->p2p_type ) {
bp_activity_add( array(
'item_id' => $p2p_id,
'user_id' => '1093',
'component' => 'activity',
'type' => 'tweet_connected',
'action' => 'test',
'content' => 'posted a new tweet'
) );
}
}
add_action( 'p2p_created_connection', 'handle_new_connection' );
Of course, tweets_to_posts must be replaced with your own connection name.
My question is, how do I go about doing the very opposite; deleting an item from the BP Stream.
I tried the following with no luck:
function handle_deleted_connection( $p2p_id ) {
$connection = p2p_get_connection( $p2p_id );
if ( 'tweets_to_posts' == $connection->p2p_type ) {
bp_activity_delete_by_item_id( array(
'item_id' => $p2p_id,
'user_id' => '1093'
) );
}
}
add_action( 'p2p_delete_connection', 'handle_deleted_connection' );
Your help is much appreciated.
The action hook is
p2p_delete_connections
not
p2p_delete_connection.
Thanks for a great plugin.
Still one question: 🙂
Can I retrieve p2p_from/to IDs in a manner similar to:
$connection = p2p_get_connection( $p2p_id );