bp_core_record_activity()

Listener function for the logged-in user’s ‘last_activity’ metadata.

Description

Many functions use a "last active" feature to show the length of time since the user was last active. This function will update that time as a usermeta setting for the user every 5 minutes while the user is actively browsing the site.

Return

(false|null) Returns false if there is nothing to do.

Source

File: bp-core/bp-core-functions.php

1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
function bp_core_record_activity() {
 
    // Bail if user is not logged in.
    if ( ! is_user_logged_in() ) {
        return false;
    }
 
    // Get the user ID.
    $user_id = bp_loggedin_user_id();
 
    // Bail if user is not active.
    if ( bp_is_user_inactive( $user_id ) ) {
        return false;
    }
 
    // Get the user's last activity.
    $activity = bp_get_user_last_activity( $user_id );
 
    // Make sure it's numeric.
    if ( ! is_numeric( $activity ) ) {
        $activity = strtotime( $activity );
    }
 
    // Get current time.
    $current_time = bp_core_current_time( true, 'timestamp' );
 
    // Use this action to detect the very first activity for a given member.
    if ( empty( $activity ) ) {
 
        /**
         * Fires inside the recording of an activity item.
         *
         * Use this action to detect the very first activity for a given member.
         *
         * @since BuddyPress 1.6.0
         *
         * @param int $user_id ID of the user whose activity is recorded.
         */
        do_action( 'bp_first_activity_for_member', $user_id );
    }
 
    // If it's been more than 5 minutes, record a newer last-activity time.
    if ( empty( $activity ) || ( $current_time >= strtotime( '+5 minutes', $activity ) ) ) {
        bp_update_user_last_activity( $user_id, date( 'Y-m-d H:i:s', $current_time ) );
    }
}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.