Problem: Wordpress multisite only allows the Admin account to include images in html for security reasons. It is possible to install a plugin that turns off these restrictions, but it apparently opens up a whole bunch of security holes, so I'd rather avoid that. I've already got bbpress bbcode and bbpress shortcode whitelist plugins installed, which means user can include images with [img]http://some.url/to/an/image[/img]. However, in the absense of a good bbcode toolbar for wordpress comments, I'd also like to use the MDC Comment Toolbar plugin, which uses html for inserting images. Because in a single site install authors can include html in comments, it shows them the 'Add Media' button. But because its multi-site, this then gets deleted.

Partial Solution: I hit upon the idea of writing a plugin that uses the preprocess_comment hook to find img html tags and convert them into bbcodes. But so far I'm struggling to get this working. Here is the contents of my plugin so far. It uses XSLTProcessor but I'm open to other ideas...

function html2bbcode( $commentdata ) {
    $xhtml = $commentdata['comment_content'];

    $xsl = <<<'EOB'
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text">
    <xsl:template match="img">[img="<xls:value-of select="@alt"/>"]<xls:value-of select="@src"/>[/img]</xsl:template>

    $xmldoc = DOMDocument::loadHTML($xhtml);
    $xsldoc = DOMDocument::loadXML($xsl);

    $proc = new XSLTProcessor();
    $xhtml = $proc->transformToXML($xmldoc);

    $commentdata['comment_content'] = $xhtml;
    return $commentdata;
add_filter( 'preprocess_comment' , 'html2bbcode' );

I'm really not convinced I've got the xsl right. It seems to produce an empty string whatever the input. But the solution doesn't have to be with xsl. Anything that works.

1 Answer 1


I eventually solved this with a slightly different approach. This allows users to include images in their comments. One issue I'm still having, which is really a whole new question, but worth mentioning here, is that the user may not be allowed to upload new images to the post, and only include images already in the library.

function html2bbcode( $commentdata ) {
  if(!is_admin()) {
    $xhtml = $commentdata['comment_content'];

    $parsed_comment = '';

    $dom = new DOMDocument();
    $img_tags = $dom->getElementsByTagName('img');

    foreach ($img_tags as $img) {
            $src = (string) $img->getAttribute('src');
            if($src == '') {
                // No image src, no worries. 
            } else {
                $src = str_replace('"', "", $src);
                $alt = (string) $img->getAttribute('alt');
                $img_class = (string) $img->getAttribute('class');

                $bbcode = ' [img='.$alt.']'.$src.'[/img] ';

                    $divNode = $dom->createElement('div');
                    $textNode = $dom->createTextNode($bbcode);

                    $img->parentNode->replaceChild($divNode, $img);            
    $parsed_comment = $dom->saveHTML();
    $parsed_comment = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">','',$parsed_comment);

    $commentdata['comment_content'] = $parsed_comment;

  return $commentdata;
add_filter( 'preprocess_comment' , 'html2bbcode' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.