oEmbed in WordPress Comments
The code below allows using oEmbed in WordPress comments.
GitHub<?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' ) );
Correction for AJAX functionality:
<?php
/**
* Class oEmbed_Comments
* @package RusDTP
*
* @see https://gist.github.com/sheabunge/6018753
*/
class oEmbed_Comments {
public function __construct() {
add_action( 'init', [ $this, 'add_filter' ] );
}
/**
* Setup filter with correct priority to enable oEmbed in comments
*/
public function add_filter() {
global $pagenow;
$access_page = [ 'index.php', 'admin-ajax.php' ];
if ( ! in_array( $pagenow, $access_page ) ) {
return;
}
/* make_clickable breaks oEmbed regex, make sure we go earlier */
remove_filter( 'comment_text', 'make_clickable', 9 );
add_filter( 'comment_text', 'make_clickable', 12 );
add_filter( 'comment_text', [ $this, 'oembed_filter' ], 11 );
}
/**
* Safely add oEmbed media to a comment
*
* @param string $comment_text The current comment text
*
* @return string The modified comment text
* @since 1.0
*/
public 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;
}
}
new oEmbed_Comments();—
Note embeded into: oEmbed in WordPress