トリコロールな猫

猫とつくばと茨城をこよなく愛するnekotricolorのブログです

WordpressのコメントのHTMLタグを禁止する

Wordpressでは、コメントで使えるHTMLタグが決まっており、許可されたタグ以外はすべて削除されてしまいます。 Wordpressネタを書いている以上、コメントでHTMLやPHPのコードを書くこともあるかもしれませんので、HTMLタグはすべてエスケープして表示するようにしました。

エスケープする関数はfunctions.php

PHPには、HTML的に特殊な文字(、&など)をHTMLとして解釈せずそのまま表示できるように変換する(エスケープする)htmlspecialchars()という便利な関数があります。 この関数を通すと、たとえば「<」が「<」になったりするわけですね。

コメントの本文はcomment_text()関数によって取得されますので、

  1. HTMLタグをエスケープする関数を作成
  2. comment_text()の結果を1.の関数に通す(フィルタリングする)

ことで、コメント本文のエスケープが可能になります。

ということで、functions.phpに以下を追加します。

function html_to_text($comment_content) {
  if ( get_comment_type() == 'comment' ) {
    $comment_content = htmlspecialchars($comment_content, ENT_QUOTES);
  }
  return $comment_content;
}
add_filter('comment_text', 'html_to_text', 9);

1~6行目がHTMLタグをエスケープする関数、7行目でcomment_text()の結果をエスケープする関数に通しています。

タグが使えないことをコメント欄に表示

コメントフォームの下にある「次のHTML タグと属性が使えます: (略)」というのを、「HTMLタグは使えません」に変更しておきます。

先日の「WordPressのコメントフォーム(comment_form)をカスタマイズ」でもやった、comment_form()の引数の設定ですね。

comment.phpにある、comment_form()の引数を追加します。 今はこう。

<?php
// デフォルト値取得
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );

// $fields設定
$fields = array(
    'author' => '<p id="inputtext">' . '<label for="author">' . __( 'Name' ) 
                . ( $req ? '(必須)' : '' ) . '</label> ' .
                '<br /><input id="author" name="author" type="text" value="' 
                . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',

    'email'  => '<p id="inputtext"><label for="email">' . __( 'Email' ) 
                . ( $req ? '(必須/公開はされません)' : '' ) . '</label> ' .
                '<br /><input id="email" name="email" type="text" value="' 
                . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',

    'url'    => '<p id="inputtext"><label for="url">' . __( 'Website' ) . '</label>' .
                '<br /><input id="url" name="url" type="text" value="' 
                . esc_attr( $commenter['comment_author_url'] ) . '" size="60" /></p>',
    ); 

// $comment_field設定
$comment_field = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '(必須)</label><br /><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';

// $comment_notes_before設定
$comment_notes_before = NULL;

// $title_reply設定
$title_reply = '<p class="comment-title">'. __( 'Leave a Reply' ). '</p>';

// $args設定
$args = array(
    'fields'       => apply_filters( 'comment_form_default_fields', $fields ),
    'comment_field'        => $comment_field,
    'comment_notes_before'     => $comment_notes_before,
    'title_reply'      => $title_reply
);
?>

<?php comment_form($args); ?>

これをこう。30、31、42行目を追加。

<?php
// デフォルト値取得
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );

// $fields設定
$fields = array(
    'author' => '<p id="inputtext">' . '<label for="author">' . __( 'Name' ) 
                . ( $req ? '(必須)' : '' ) . '</label> ' .
                '<br /><input id="author" name="author" type="text" value="' 
                . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',

    'email'  => '<p id="inputtext"><label for="email">' . __( 'Email' ) 
                . ( $req ? '(必須/公開はされません)' : '' ) . '</label> ' .
                '<br /><input id="email" name="email" type="text" value="' 
                . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',

    'url'    => '<p id="inputtext"><label for="url">' . __( 'Website' ) . '</label>' .
                '<br /><input id="url" name="url" type="text" value="' 
                . esc_attr( $commenter['comment_author_url'] ) . '" size="60" /></p>',
    ); 

// $comment_field設定
$comment_field = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '(必須)</label><br /><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';

// $comment_notes_before設定
$comment_notes_before = NULL;

// $comment_notes_after設定
$comment_notes_after = '<p class="form-allowed-tags">※HTMLタグは使えません</p>';

// $title_reply設定
$title_reply = '<p class="comment-title">'. __( 'Leave a Reply' ). '</p>';

// $args設定
$args = array(
    'fields'       => apply_filters( 'comment_form_default_fields', $fields ),
    'comment_field'        => $comment_field,
    'comment_notes_before'     => $comment_notes_before,
    'title_reply'      => $title_reply,
    'comment_notes_after'  => $comment_notes_after
);
?>

<?php comment_form($args); ?>

これで、タグを入力しても解釈されずにそのままテキストとして表示されるようになります。

参照