BP_Media_Extractor::extract( string|WP_Post $richtext, int $what_to_extract = self::ALL, array $extra_args = array() )

Extract media from text.

Description

Parameters

$richtext

(string|WP_Post) (Required) Content to parse.

$what_to_extract

(int) (Optional) Media type to extract (optional).

Default value: self::ALL

$extra_args

(array) (Optional) Bespoke data for a particular extractor (optional).

Default value: array()

Return

(array)

  • 'has'
    (array) Extracted media counts. {
  • 'audio'
    (int)
  • 'embeds'
    (int)
  • 'images'
    (int)
  • 'links'
    (int)
  • 'mentions'
    (int)
  • 'shortcodes'
    (int)
  • 'video'
    (int)
  • 'audio'
    (array) Extracted audio. { Array of extracted media.
  • 'source'
    (string) Media source. Either "html" or "shortcodes".
  • 'url'
    (string) Link to audio.
  • 'embeds'
    (array) Extracted oEmbeds. { Array of extracted media.
  • 'url'
    (string) oEmbed link.
  • 'images'
    (array) Extracted images. { Array of extracted media.
  • 'gallery_id'
    (int) Gallery ID. Optional, not always set.
  • 'height'
    (int) Width of image. If unknown, set to 0.
  • 'source'
    (string) Media source. Either "html" or "galleries".
  • 'url'
    (string) Link to image.
  • 'width'
    (int) Width of image. If unknown, set to 0.
  • 'links'
    (array) Extracted URLs. { Array of extracted media.
  • 'url'
    (string) Link.
  • 'mentions'
    (array) Extracted mentions. { Array of extracted media.
  • 'name'
    (string) @mention.
  • 'user_id'
    (string) User ID. Optional, only set if Activity component enabled.
  • 'shortcodes'
    (array) Extracted shortcodes. { Array of extracted media.
  • 'attributes'
    (array) Key/value pairs of the shortcodes attributes (if any).
  • 'content'
    (string) Text wrapped by the shortcode.
  • 'type'
    (string) Shortcode type.
  • 'original'
    (string) The entire shortcode.
  • 'videos'
    (array) Extracted video. { Array of extracted media.
  • 'source'
    (string) Media source. Currently only "shortcodes".
  • 'url'
    (string) Link to audio.
  • Source

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

    	public function extract( $richtext, $what_to_extract = self::ALL, $extra_args = array() ) {
    		$media = array();
    
    		// Support passing a WordPress Post for the $richtext parameter.
    		if ( is_a( $richtext, 'WP_Post' ) ) {
    			$extra_args['post'] = $richtext;
    			$richtext           = $extra_args['post']->post_content;
    		}
    
    		$plaintext = $this->strip_markup( $richtext );
    
    
    		// Extract links.
    		if ( self::LINKS & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_links( $richtext, $plaintext, $extra_args ) );
    		}
    
    		// Extract mentions.
    		if ( self::MENTIONS & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_mentions( $richtext, $plaintext, $extra_args ) );
    		}
    
    		// Extract images.
    		if ( self::IMAGES & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_images( $richtext, $plaintext, $extra_args ) );
    		}
    
    		// Extract shortcodes.
    		if ( self::SHORTCODES & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_shortcodes( $richtext, $plaintext, $extra_args ) );
    		}
    
    		// Extract oEmbeds.
    		if ( self::EMBEDS & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_embeds( $richtext, $plaintext, $extra_args ) );
    		}
    
    		// Extract audio.
    		if ( self::AUDIO & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_audio( $richtext, $plaintext, $extra_args ) );
    		}
    
    		// Extract video.
    		if ( self::VIDEOS & $what_to_extract ) {
    			$media = array_merge_recursive( $media, $this->extract_video( $richtext, $plaintext, $extra_args ) );
    		}
    
    		/**
    		 * Filters media extracted from text.
    		 *
    		 * @since BuddyPress 2.3.0
    		 *
    		 * @param array  $media           Extracted media. See {@link BP_Media_Extractor::extract()} for format.
    		 * @param string $richtext        Content to parse.
    		 * @param int    $what_to_extract Media type to extract.
    		 * @param array  $extra_args      Bespoke data for a particular extractor.
    		 * @param string $plaintext       Copy of $richtext without any markup.
    		 */
    		return apply_filters( 'bp_media_extractor_extract', $media, $richtext, $what_to_extract, $extra_args, $plaintext );
    	}
    

    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.