Something like this should work to remove oneboxes from comments. It’s assuming that the libxml
extension is loaded on your server. (It probably is.)
add_filter( 'wpdc_comment_body', 'wpdc_custom_comment_body' );
function wpdc_custom_comment_body( $content ) {
// Allows parsing misformed html. Save the previous value of libxml_use_internal_errors so that it can be restored.
$use_internal_errors = libxml_use_internal_errors( true );
$doc = new \DOMDocument( '1.0', 'utf-8' );
$doc->loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ) );
$finder = new \DOMXPath( $doc );
$oneboxes = $finder->query( "//*[contains(@class, 'onebox')]");
foreach( $oneboxes as $onebox ) {
$onebox->parentNode->removeChild( $onebox );
}
// Clear the libxml error buffer.
libxml_clear_errors();
// Restore the previous value of libxml_use_internal_errors.
libxml_use_internal_errors( $use_internal_errors );
$parsed = $doc->saveHTML( $doc->documentElement );
// Remove DOCTYPE, html, and body tags that have been added to the DOMDocument.
$parsed = preg_replace( '~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $parsed );
return $parsed;
}
The Discourse plugin uses DOMDocument
to fix relative links that are returned from Discourse. That code is here: https://github.com/discourse/wp-discourse/blob/master/templates/template-functions.php#L71