wp_get_note_mentioned_user_ids( string $content ): int[]

Extracts the mentioned user IDs from note content.

Description

Mentions are stored as chips carrying the wp-note-mention class plus a user-N class token holding the mentioned user’s ID: <span class="wp-note-mention user-N">@Name</span>. Only elements that carry both classes are treated as mentions.

Parameters

$contentstringrequired
Note (comment) content, as stored.

Return

int[] Unique, positive mentioned user IDs.

Source

function wp_get_note_mentioned_user_ids( string $content ): array {
	if ( ! str_contains( $content, 'wp-note-mention' ) ) {
		return array();
	}

	$user_ids  = array();
	$processor = new WP_HTML_Tag_Processor( $content );
	while (
		$processor->next_tag(
			array(
				'tag_name'   => 'SPAN',
				'class_name' => 'wp-note-mention',
			)
		)
	) {
		foreach ( $processor->class_list() as $class_name ) {
			if ( 1 === preg_match( '/^user-(\d+)$/', $class_name, $matches ) ) {
				$user_id = (int) $matches[1];
				if ( $user_id > 0 ) {
					$user_ids[] = $user_id;
				}
				break;
			}
		}
	}

	return array_values( array_unique( $user_ids, SORT_NUMERIC ) );
}

Changelog

VersionDescription
7.1.0Introduced.

User Contributed Notes

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

Sponsor
SponsoredKunjungi sekarang
Promo