Using the WordPress get_media_embedded_in_content function

The get_media_embedded_in_content function in WordPress extracts media URLs embedded within the content of a post or page. This function scans the provided content for media elements and returns an array of media URLs that are found.

This function can be utilized in scenarios where there is a need to retrieve and manipulate media elements separately from the rest of the content. It can help in isolating media items for tasks such as creating media galleries, performing analytics on embedded media, or generating media-specific reports.

The function identifies various types of media, such as:

  • Images
  • Audio files
  • Video files

By using get_media_embedded_in_content, developers can efficiently extract media elements from content, facilitating further processing or display as required by the application.

Parameters

  • $content (string), required. A string of HTML that may contain media elements.
  • $types (string[]), optional. Default value: null. An array specifying media types such as ‘audio’, ‘video’, ‘object’, ’embed’, or ‘iframe’.

Return Value

The function returns an array of strings, each representing an HTML media element found within the content.

Examples

How to extract all media elements from a post content

$content = get_the_content();
$media_elements = get_media_embedded_in_content( $content );

foreach ( $media_elements as $media ) {
 echo $media;
}

This snippet retrieves the content of the current post using get_the_content(), then extracts all media elements from the content using get_media_embedded_in_content(). The media elements are then echoed out within a foreach loop.

How to extract only video elements from a post content

$content = get_the_content();
$media_elements = get_media_embedded_in_content( $content, array( 'video' ) );

foreach ( $media_elements as $media ) {
 echo $media;
}

This snippet retrieves the content of the current post using get_the_content(), then extracts only video elements from the content by passing an array with ‘video’ as the second parameter to get_media_embedded_in_content(). The video elements are then echoed out within a foreach loop.

How to extract both audio and embed elements from a post content

$content = get_the_content();
$media_elements = get_media_embedded_in_content( $content, array( 'audio', 'embed' ) );

foreach ( $media_elements as $media ) {
 echo $media;
}

This snippet retrieves the content of the current post using get_the_content(), then extracts both audio and embed elements from the content by passing an array with ‘audio’ and ’embed’ as the second parameter to get_media_embedded_in_content(). The audio and embed elements are then echoed out within a foreach loop.

Conclusion

The get_media_embedded_in_content function provides a streamlined method for extracting media elements embedded within post content. This function scans the content for media shortcodes and returns an array of the embedded media HTML. It is particularly useful for developers aiming to manipulate or display media elements separately from the primary content. By leveraging this function, developers can efficiently manage and utilize embedded media, enhancing the flexibility and control over content presentation within WordPress themes and plugins.

Related WordPress Functions