bp_activity_find_mentions( string $content )

Locate usernames in an activity content string, as designated by an @ sign.


Parameters Parameters

$content

(string) (Required) The content of the activity, usually found in $activity->content.


Top ↑

Return Return

(array|bool) Associative array with user ID as key and username as value. Boolean false if no mentions found.


Source Source

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

function bp_activity_find_mentions( $content ) {

	$pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
	preg_match_all( $pattern, $content, $usernames );

	// Make sure there's only one instance of each username.
	$usernames = array_unique( $usernames[1] );

	// Bail if no usernames.
	if ( empty( $usernames ) ) {
		return false;
	}

	$mentioned_users = array();

	// We've found some mentions! Check to see if users exist.
	foreach( (array) array_values( $usernames ) as $username ) {
		$user_id = bp_activity_get_userid_from_mentionname( $username );

		// The user ID exists, so let's add it to our array.
		if ( ! empty( $user_id ) ) {
			$mentioned_users[ $user_id ] = $username;
		}
	}

	if ( empty( $mentioned_users ) ) {
		return false;
	}

	/**
	 * Filters the mentioned users.
	 *
	 * @since 2.5.0
	 *
	 * @param array $mentioned_users Associative array with user IDs as keys and usernames as values.
	 */
	return apply_filters( 'bp_activity_mentioned_users', $mentioned_users );
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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