wp_get_note_mentioned_user_ids()
Extracts the mentioned user IDs from note content.
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.
No Hooks.
Returns
int[]. Unique, positive mentioned user IDs.
Usage
wp_get_note_mentioned_user_ids( $content ): array;
- $content(string) (required)
- Note (comment) content, as stored.
Changelog
| Since 7.1.0 | Introduced. |
wp_get_note_mentioned_user_ids() wp get note mentioned user ids code WP 7.1
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 ) );
}