bbp_reply_content_autoembed_paragraph( $content )

Add oembed to forum reply.

Description

Parameters

$content

(Required)

Return

(string)

Source

File: bp-forums/replies/functions.php

1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
function bbp_reply_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.