WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
Adding New Post Connection to Activity Stream (7 posts)

  1. Shaun McMillan
    Member
    Posted 1 year ago #

    Would it be possible to add an update to the buddypress activity stream when a new connection is made?

    My guess is that you could use the bp_activity_add() function. Would I add the code into the my_connection_types() function or maybe hook a new function into the p2p_init action?

    Here's the scenario. A user earns a badge (custom post-type connected to a user) and then in the activity stream it says, "Congratulation to [user fullname], who has just earned the [badge name] badge!"

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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' );
  3. Shaun McMillan
    Member
    Posted 1 year ago #

    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.

  4. Shaun McMillan
    Member
    Posted 1 year ago #

    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' );
  5. blurb
    Member
    Posted 1 year ago #

    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.

  6. blurb
    Member
    Posted 1 year ago #

    The action hook is

    p2p_delete_connections

    not

    p2p_delete_connection.

    Thanks for a great plugin.

  7. blurb
    Member
    Posted 1 year ago #

    Still one question: :)

    Can I retrieve p2p_from/to IDs in a manner similar to:

    $connection = p2p_get_connection( $p2p_id );

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic