Skip to content

Instantly share code, notes, and snippets.

@sheabunge
Created July 17, 2013 08:18
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sheabunge/6018753 to your computer and use it in GitHub Desktop.
Allows the use of oEmbed in WordPress comments
<?php
/**
* Plugin Name: oEmbed in Comments
* Description: Allow oEmbeds in comment text. A fork of http://wordpress.org/plugins/oembed-in-comments/
* Version: 1.2
* Author: Evan Solomon, modified by Shea Bunge
*/
class oEmbed_Comments {
/**
* Setup filter with correct priority to do oEmbed in comments
* @since 1.0
* @uses is_admin() To make sure we don't do anything in the admin
* @uses add_filter To register a filter hook
* @uses has_filter() To check if a filter is registered
* @return void
*/
static function add_filter() {
if ( is_admin() )
return;
/* make_clickable breaks oEmbed regex, make sure we go earlier */
$clickable = has_filter( 'comment_text', 'make_clickable' );
$priority = ( $clickable ) ? $clickable - 1 : 10;
add_filter( 'comment_text', array( __CLASS__, 'oembed_filter' ), $priority );
}
/**
* Safely add oEmbed media to a comment
* @since 1.0
* @param string $comment_text The current comment test
* @return string The modified comment text
*/
static function oembed_filter( $comment_text ) {
global $wp_embed;
/* Automatic discovery would be a security risk, safety first */
add_filter( 'embed_oembed_discover', '__return_false', 999 );
$comment_text = $wp_embed->autoembed( $comment_text );
/* ...but don't break your posts if you use it */
remove_filter( 'embed_oembed_discover', '__return_false', 999 );
return $comment_text;
}
}
add_action( 'init', array( 'oEmbed_Comments', 'add_filter' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment