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

Extract shortcodes from text.


Description Description

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


Parameters 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()


Top ↑

Return 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.

  • Top ↑

    Source 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 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 );
    	}
    

    Top ↑

    Changelog Changelog

    Changelog
    Version Description
    2.3.0 Introduced.

    Top ↑

    User Contributed Notes User Contributed Notes

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