bp_update_user_last_activity( int $user_id, string $time = '' )

Update a user’s last activity.


Parameters Parameters

$user_id

(int) (Required) ID of the user being updated.

$time

(string) (Optional) Time of last activity, in 'Y-m-d H:i:s' format.

Default value: ''


Top ↑

Return Return

(bool) True on success, false on failure.


Source Source

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

function bp_update_user_last_activity( $user_id = 0, $time = '' ) {

	// Fall back on current user.
	if ( empty( $user_id ) ) {
		$user_id = bp_loggedin_user_id();
	}

	// Bail if the user id is 0, as there's nothing to update.
	if ( empty( $user_id ) ) {
		return false;
	}

	// Fall back on current time.
	if ( empty( $time ) ) {
		$time = bp_core_current_time();
	}

	/** This filter is documented in bp_core_get_users() */
	$use_legacy_query = apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, [ 'user_id' => $user_id ] );

	/*
	 * As of BuddyPress 2.0, last_activity is no longer stored in usermeta.
	 * However, we mirror it there for backward compatibility. Do not use!
	 *
	 * As of BuddyPress 7.0, mirroring is only allowed if the legacy user
	 * query is enabled.
	 */
	if ( $use_legacy_query ) {
		remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10 );
		remove_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10 );
		bp_update_user_meta( $user_id, 'last_activity', $time );
		add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
		add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 4 );
	}

	return BP_Core_User::update_last_activity( $user_id, $time );
}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Backward compatibility usermeta mirroring is only allowed if the legacy user query is enabled.
1.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.