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

(Required) The content to be searched.

Return

(string) Potentially modified $content.

Source

File: bp-core/classes/class-bp-embed.php

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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.