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

Extract shortcodes from text.

Description

This includes any shortcodes indirectly used by other media extraction types. For example, and .

Parameters

$richtext

(Required) Content to parse.

$plaintext

(Required) Sanitized version of the content.

$extra_args

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

Default value: array()

Return

(array)

  • 'has'
    (array) Extracted media counts. {
  • 'shortcodes'
    (int)
  • '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.
  • Source

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

    	protected function extract_shortcodes( $richtext, $plaintext, $extra_args = array() ) {
    		$data = array( 'has' => array( 'shortcodes' => 0 ), 'shortcodes' => array() );
    
    		// Match any registered WordPress shortcodes.
    		if ( strpos( $richtext, '[' ) !== false ) {
    			preg_match_all( '/' . get_shortcode_regex() . '/s', $richtext, $matches );
    
    			if ( ! empty( $matches[2] ) ) {
    				foreach ( $matches[2] as $i => $shortcode_name ) {
    					$attrs = shortcode_parse_atts( $matches[3][ $i ] );
    					$attrs = ( ! $attrs ) ? array() : (array) $attrs;
    
    					$shortcode               = array();
    					$shortcode['attributes'] = $attrs;             // Attributes.
    					$shortcode['content']    = $matches[5][ $i ];  // Content.
    					$shortcode['type']       = $shortcode_name;    // Shortcode.
    					$shortcode['original']   = $matches[0][ $i ];  // Entire shortcode.
    
    					$data['shortcodes'][] = $shortcode;
    				}
    			}
    		}
    
    		$data['has']['shortcodes'] = count( $data['shortcodes'] );
    
    		/**
    		 * Filters shortcodes extracted from text.
    		 *
    		 * @since BuddyPress 2.3.0
    		 *
    		 * @param array  $data       Extracted shortcodes.
    		 *                           See {@link BP_Media_Extractor::extract_shortcodes()} 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.
    		 */
    		return apply_filters( 'bp_media_extractor_shortcodes', $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.