bbp_topic_content_autoembed_paragraph( $content )

Add oembed to forum topic.

Description

Parameters

$content

(Required)

Return

(string)

Source

File: bp-forums/topics/functions.php

3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
function bbp_topic_content_autoembed_paragraph( $content ) {
 
    // 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 ) {
                // Fetch the oembed code for URL.
                $embed_code = wp_oembed_get( $url );
                // 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;
        }
    }
}

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.