BP_Media_Extractor::extract_mentions( string $richtext, string $plaintext, array $extra_args = array() )

Extract @mentions tags from text.

Description

If the Activity component is enabled, it is used to parse @mentions. The mentioned "name" must match a user account, otherwise it is discarded.

If the Activity component is disabled, any @mentions are extracted.

Parameters

$richtext

(Required) Content to parse.

$plaintext

(Required) Sanitized version of the content.

$extra_args

(Optional) Bespoke data for a particular extractor.

Default value: array()

Return

(array)

  • 'has'
    (array) Extracted media counts. {
  • 'mentions'
    (int)
  • 'mentions'
    (array) Extracted mentions. { Array of extracted media.
  • 'name'
    (string) @mention.
  • 'user_id'
    (string) User ID. Optional, only set if Activity component enabled.
  • Source

    File: bp-core/classes/class-bp-media-extractor.php

    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    protected function extract_mentions( $richtext, $plaintext, $extra_args = array() ) {
        $data     = array( 'has' => array( 'mentions' => 0 ), 'mentions' => array() );
        $mentions = array();
     
        // If the Activity component is active, use it to parse @mentions.
        if ( bp_is_active( 'activity' ) ) {
            $mentions = bp_activity_find_mentions( $plaintext );
            if ( ! $mentions ) {
                $mentions = array();
            }
     
        // If the Activity component is disabled, instead do a basic parse.
        } else {
            if ( strpos( $plaintext, '@' ) !== false ) {
                preg_match_all( '/[@]+([A-Za-z0-9-_\.@]+)\b/', $plaintext, $matches );
     
                if ( ! empty( $matches[1] ) ) {
                    $mentions = array_unique( array_map( 'strtolower', $matches[1] ) );
                }
            }
        }
     
        // Build results.
        foreach ( $mentions as $user_id => $mention_name ) {
            $mention = array( 'name' => strtolower( $mention_name ) );
     
            // If the Activity component is active, store the User ID, too.
            if ( bp_is_active( 'activity' ) ) {
                $mention['user_id'] = (int) $user_id;
            }
     
            $data['mentions'][] = $mention;
        }
     
        $data['has']['mentions'] = count( $data['mentions'] );
     
        /**
         * Filters @mentions extracted from text.
         *
         * @since BuddyPress 2.3.0
         *
         * @param array  $data       Extracted @mentions. See {@link BP_Media_Extractor::extract_mentions()} for format.
         * @param string $richtext   Content to parse.
         * @param string $plaintext  Copy of $richtext without any markup.
         * @param array  $extra_args Bespoke data for a particular extractor (optional).
         */
        return apply_filters( 'bp_media_extractor_mentions', $data, $richtext, $plaintext, $extra_args );
    }

    Changelog

    Changelog
    Version Description
    BuddyPress 2.3.0 Introduced.

    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.