Skip to content

Instantly share code, notes, and snippets.

@sbruner
Created November 20, 2023 00:34
Show Gist options
  • Save sbruner/5f4038c04789fffa85ab4c24b8a4edd7 to your computer and use it in GitHub Desktop.
Save sbruner/5f4038c04789fffa85ab4c24b8a4edd7 to your computer and use it in GitHub Desktop.
oEmbed in Comments
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