Created
November 20, 2023 00:34
-
-
Save sbruner/5f4038c04789fffa85ab4c24b8a4edd7 to your computer and use it in GitHub Desktop.
oEmbed in Comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sfire_embed_oembed_in_comments( $comment_text ) { | |
// Extract URLs from the comment text | |
$urls = wp_extract_urls( $comment_text ); | |
// Process each URL | |
foreach ( $urls as $url ) { | |
// Check if the URL can be embedded | |
$embed_html = wp_oembed_get( $url ); | |
// echo $embed_html; | |
if ( $embed_html ) { | |
$full_original_url = '<a href="' . $url . '" rel="nofollow ugc">' . $url . '</a>'; | |
// Replace the URL with the embed code | |
$comment_text = str_replace( $full_original_url, $embed_html, $comment_text ); | |
} | |
} | |
return $comment_text; | |
} | |
add_filter('comment_text', 'sfire_embed_oembed_in_comments'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment