Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add New User Activity on Button Click

  • @sjs719

    Participant

    Hey guys,

    I have a button on the user profile page which is NOT an XProfile field which I need to add to activity. So when the user clicks the button a new activity will be created in their activity stream. I have successfully added a custom post type to the activity stream so when the user creates a post it shows in activity, but I cannot figure out how to apply the same function I used for the custom post type to the button click.

    Here is my custom post type function:

    // add custom post type listing to the activity stream
    
    add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_custom_post_types',1,1 );
    function activity_publish_custom_post_types( $post_types ) {
    $post_types[] = 'listing';
    return $post_types;
    }
    
    add_filter('bp_blogs_activity_new_post_action', 'record_cpt_activity_action', 1, 3);
    function record_cpt_activity_action( $activity_action, $post, $post_permalink ) {
    global $bp;
    if( $post->post_type == 'listing' ) {
    
    $activity_action = sprintf( __( '%1$s added a new listing - %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '' . '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' . '' );
    }
    
    return $activity_action;
    }  

    I really need to implement this so any help is greatly appreciated!

Viewing 18 replies - 1 through 18 (of 18 total)
  • @shanebp

    Moderator

    On button click, use a page refresh or ajax to fire a function that calls bp_activity_add.

    @sjs719

    Participant

    Thanks for the reply shane, I have tried adding an onChange and onClick event to the checkbox called “bp_activity_add()” but it breaks the button because it doesn’t recognize the function “bp_activity_add()”.

    Do I need to add something to functions.php as well as the onChange event? If you could provide me with an example I would REALLY appreciate it!
    Thx

    @sjs719

    Participant

    I think you are telling me to add add a function such as this:

    function bp_custom_record_activity() {
     
        $activity_id = bp_activity_add( array(
            'id'		    => '',
            'action'            => '',
            'content'           => '',
            'component'         => '',
            'type'              => '',
            'primary_link'	    => '',
            'user_id'	    => '',
            'item_id'	    => '',
            'secondary_item_id' => '',
            'recorded_time'	    => '',
            'hide_sitewide'	    => '',
        ) );
     
        return $activity_id;
    }

    And then call that function onChange but I am not sure which params I need to include and what the value of those parameters needs to be. I read the bp_activity_add codex but I’m still a little confused.

    bp_activity_add()

    If I have the following checkbox, what values should I use for params such as item_id, action, ID, etc:

    <div id="bd-button">   
    <form name="bdchkbox">
    <label>  
      <input type="checkbox" hidden="hidden" name="bdchk" id="bdchk" onChange="set_check(this);submit()"><span id="bdlbl">Featured</span> 
    </label>
    </form>
    </div>

    @sjs719

    Participant

    Anyone?

    @shanebp

    Moderator

    Yes you need a function.
    To call the function on change, you need to use ajax.
    https://codex.wordpress.org/AJAX

    @sjs719

    Participant

    @shanebp

    I understand that I need to create and call a function, I have tried using the following code and no activity gets registered:

    function bp_custom_record_activity() {
     
        $activity_id = bp_activity_add( array(
            'id'		    => '',
            'action'            => '',
            'content'           => '',
            'component'         => '',
            'type'              => '',
            'primary_link'	    => '',
            'user_id'	    => '',
            'item_id'	    => '',
            'secondary_item_id' => '',
            'recorded_time'	    => '',
            'hide_sitewide'	    => '',
        ) );
     
        return $activity_id;
    }

    I have tried multiple values in each field but cannot get it working. So if this is the correct function to use then which values must be filled?
    thx

    @sjs719

    Participant

    bump

    @sjs719

    Participant

    bump2

    @sjs719

    Participant

    Anyone? Seems like something that many others would have come across…

    @turtle09

    Participant

    Did you figure out how to add a custom activity?

    @sjs719

    Participant

    No I can’t get any support…

    @coffeywebdev

    Participant

    Take a look at this code, I think it may be helpful for you…

    I created a function vp_add_group_activity_comments() that hooks to the ‘post_comment’ action, so when a user posts a comment an activity stream item is created and added to their group’s feed.. (on my site users can only be in one group at a time)

    It’s all about what you set for the $args array….

    
    function vp_add_group_activity_comments($comment_id){
    
    $comment = get_comment( $comment_id );
    GLOBAL $wpdb;
    $user_id = get_current_user_id();
    $user_info = get_userdata( $user_id );
    
    $current_user_group_id = $wpdb->get_var("SELECT group_id FROM ".$wpdb->prefix."bp_groups_members WHERE user_id = '".(int)$user_id."'");
    
    $comment_action = "<a href='".site_url()."/members/".$user_info->user_login."'>".$user_info->first_name." ".$user_info->last_name."</a> posted a comment on <a href='".get_permalink($comment->comment_post_ID )."'>".get_the_title($comment->comment_post_ID)."</a>";
    
    // arguments to pass to the bp_activity_add() function
    $args = array(
    'action'            => $comment_action,                     // The activity action - e.g. "Jon Doe posted an update"
    'content'           => $comment->comment_content,                     // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!"
    'component'         => 'groups',                  // The name/ID of the component e.g. groups, profile, mycomponent
    'type'              => 'activity_update',                  // The activity type e.g. activity_update, profile_updated
    'primary_link'      => get_permalink($comment->comment_post_ID ),                     // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink)
    'user_id'           => $user_id,  // Optional: The user to record the activity for, can be false if this activity is not for a user.
    'item_id'           => $current_user_group_id,                  // Optional: The ID of the specific item being recorded, e.g. a blog_id
    'secondary_item_id' => $comment->comment_ID,                  // Optional: A second ID used to further filter e.g. a comment_id
    'recorded_time'     => bp_core_current_time(), // The GMT time that this activity was recorded
    'hide_sitewide'     => 0,                  // Should this be hidden on the sitewide activity stream?
    'is_spam'           => 0,                  // Is this activity item to be marked as spam?
    );
    
    $add_activity = bp_activity_add($args);
    
    // Update the group's last activity
    groups_update_last_activity( $current_user_group_id );
    
    return true;
    }  
    add_action('comment_post', 'vp_add_group_activity_comments' );
    

    @sjs719

    Participant

    Hey Coffeywebdev,

    Thank you for the reply and info. I understand what code I need to use to add an activity but what I can’t figure out is how to implement a button click into this code. I can create an activity for a new post or comment but I cannot get the button action to properly register as an activity.

    When the button is clicked it creates user meta in the database named “Featured” can I use that in the add activity function? So instead of $comment = get_comment( $comment_id ); it would be something like $activity = get_user_meta ($user_id, 'featured') ?

    @coffeywebdev

    Participant

    I use the “$comment = get_comment( $comment_id )” only to get the info I need about the comment..

    For example, in my activity stream I need to know what the comment said, who said it, and what post did they comment on… So, I use the get_comment() function to store all of my comment data so that I can use it in the activity stream.

    What exactly do you want the activity to say? I’m pretty sure I can help!

    @sjs719

    Participant

    Hey Coffeywebdev,

    I really appreciate your help! Here is what I am trying to do:

    I have a checkbox in the members profile (it’s not a XProfile field) that when clicked makes their profile “Featured” for a while. When the user checks the box it creates a meta entry named “Featured” in the database. Whenever a user checks their checkbox I want it to register as an activity saying something along the lines of “user’s profile is now featured!”.

    Here is the code I am using to display the checkbox:

    <div id="featured-button">   
    <form name="feat_chkbox">
    <label>  
      <input type="checkbox" hidden="hidden" name="feat_chk" id="feat_chk"><span id="featlbl">Featured Member</span> 
    </label>
    </form>
    </div>
    

    I took out all of the onClick functions to avoid confusion. I am guessing I need to create a new onClick event which calls to the bd_activity_add() function, but I don’t know how to build that function. I have tried multiple codes using the following layout:

    function add_featured_activity(){
    global $wpdb, $bp;
    
    $user_id = get_current_user_id();
    
    $args = array(
    'action'            => 
    'content'           => 
    'component'         => 
    'type'              => 
    'primary_link'      => 
    'user_id'           => 
    'item_id'           => 
    'secondary_item_id' => 
    );
    
    $add_activity = bp_activity_add($args);
    
    add_action('comment_post', 'add_featured_activity' );

    But I must not be using the correct args or something. Do you know what args need to be used to register this activity using either the checkbox click event or the meta data created by the checkbox?

    Thx!

    @sjs719

    Participant

    For example, I have tried the following code and tried to call it onClick but it either doesn’t register or throws an error:

    function add_featured_activity(){
    
    GLOBAL $wpdb;
    $user_id = get_current_user_id();
    $user_info = get_userdata( $user_id );
    
    $button_action = "<a href='".site_url()."/members/".$user_info->user_login."'></a>";
    
    // arguments to pass to the bp_activity_add() function
    $args = array(
    'action'            => $button_action,                     // The activity action - e.g. "Jon Doe posted an update"
    'component'         => 'profile',                  // The name/ID of the component e.g. groups, profile, mycomponent
    'type'              => 'activity_update',                  // The activity type e.g. activity_update, profile_updated
    'user_id'           => $user_id,  // Optional: The user to record the activity for, can be false if this activity is not for a user.
    'item_id'           => $user_id,                  // Optional: The ID of the specific item being recorded, e.g. a blog_id
    'recorded_time'     => bp_core_current_time(), // The GMT time that this activity was recorded
    'hide_sitewide'     => 0,                  // Should this be hidden on the sitewide activity stream?
    'is_spam'           => 0,                  // Is this activity item to be marked as spam?
    );
    
    $add_activity = bp_activity_add($args);
    
    // Update the members's last activity
    update_last_activity( $user_id );
    
    return true;
    }  
    add_action('bp_activity_add', 'add_featured_activity' );

    @sjs719

    Participant

    Any thoughts @coffeywebdev?

    @turtle09

    Participant

    @sj719 @coffeywebdev

    Did either of you ever find a solution? I really need to add an activity when a button gets clicked by the user!

Viewing 18 replies - 1 through 18 (of 18 total)
  • The topic ‘Add New User Activity on Button Click’ is closed to new replies.
Skip to toolbar