BuddyDev

Search

Add an activity when a new user signs up

  • Participant
    Level: Enlightened
    Posts: 79
    Leo on #3568

    Hi guys,

    I want to add a small trick to my BP site, which is to add an activity that contains a user profile field value when a user signs up, it is kind of an introduction.
    I tried to add the field value to the new_member activity but it didn’t work, so I thought I could do it like this.

    Here’s the code snippet I added to my bp-custom.php, can you please check?

    $bp = buddypress();
    $bp->activation_complete = true;

    function xprofile_bio_field_activity() {
    global $bp;
    if ( !function_exists( 'bp_activity_add' ) )
    return false;

    // $user_id = HOW_TO_GET_THE_USER_ID();
    $userlink = bp_core_get_userlink( $user_id );

    bp_activity_add( array(
    'user_id' => $user_id,
    'action' => apply_filters( 'xprofile_bio_field_action', sprintf( __( '%s has just registered for this website', 'buddypress' ), $userlink ), $user_id ),
    'content' => bp_member_profile_data('field=Bio'),
    'component' => 'profile',
    'type' => 'new_user_bio'
    ) );
    }

    add_action( 'bp_core_activated_user', 'xprofile_bio_field_activity' );

    when a user registers now, I get such an activity:
    “has just registered for this website 10 seconds ago”

    there’s no username, no user avatar and no profile field value as I do not have user_id.

    can you please tell me how to get the user_id? and also is this correct or not?

    Thank you,
    Leo

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #3571

    Hi Leo,

    You can try this code as this action provide user_id as parameter

    
    function xprofile_bio_field_activity( $user_id ) {
    
        $bp = buddypress();
    
        if ( ! function_exists( 'bp_activity_add' ) ) {
            return false;
        }
    
        $userlink = bp_core_get_userlink( $user_id );
    
        bp_activity_add( array(
            'user_id'   => $user_id,
            'action'    => apply_filters( 'xprofile_bio_field_action', sprintf( __( '%s has just registered for this website', 'buddypress' ), $userlink ), $user_id ),
            'content'   => bp_member_profile_data('field=Bio'),
            'component' => 'profile',
            'type'      => 'new_user_bio'
        ) );
    
    }
    add_action( 'bp_core_activated_user', 'xprofile_bio_field_activity' );
    
    
  • Participant
    Level: Enlightened
    Posts: 79
    Leo on #3574

    Thank you, Ravi. I will try that and let you know.

You must be logged in to reply to this topic.

This topic is: not resolved