BP_Embed::autoembed( string $content, $type = '' )
Passes any unlinked URLs that are on their own line to WP_Embed::shortcode() for potential embedding.
Description
See also
Parameters
- $content
-
(string) (Required) The content to be searched.
Return
(string) Potentially modified $content.
Source
File: bp-core/classes/class-bp-embed.php
public function autoembed( $content, $type = '' ) { if ( isset( $type->component ) && ( 'activity_update' === $type->type || 'activity_comment' === $type->type ) ) { // Check if WordPress already embed the link then return the original content. if (strpos($content,'<iframe') !== false) { return $content; } else { // Find all the URLs from the content. preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $content, $match); // Check if URL found. if( isset( $match[0] ) ) { $html = ''; // Remove duplicate from array and run the loop foreach ( array_unique( $match[0] ) as $url ) { // Store oembed iframe in database cache for 1 day if ( false === ( $embed_code = get_transient( $url ) ) ) { // Fetch the oembed code for URL. $embed_code = wp_oembed_get( $url ); set_transient( $url, $embed_code, DAY_IN_SECONDS ); } // If oembed found then store into the $html if (strpos($embed_code,'<iframe') !== false) { $html .= '<p>'.$embed_code.'</p>'; } } // If $html blank return original content if ( '' === $html ) { return $content; // Return the new content by adding oembed after the content. } else { return $content.$html; } // Else return original content. } else { return $content; } } } // Replace line breaks from all HTML elements with placeholders. $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) ); $old_content = $content; if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) { $url = '@(http(s)?)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@'; $content = preg_replace($url, '<p>$0</p>', $content); // Find URLs on their own line. $content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content ); // Find URLs in their own paragraph. $content = preg_replace_callback( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content ); } if (strpos($content,'<iframe') !== false) { } else { $content = $old_content; } // Put the line breaks back. return str_replace( '<!-- wp-line-break -->', "\n", $content ); }
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.