wp_check_comment_disallowed_list()


WordPressの関数wp_check_comment_disallowed_list()は、コメントが禁止リストに含まれているかどうかをチェックします。

構文

wp_check_comment_disallowed_list( $author, $email, $url, $comment, $user_ip, $user_agent );

引数の説明:

  • $author (string) — コメントの著者名
  • $email (string) — コメントのメールアドレス
  • $url (string) — コメントのURL
  • $comment (string) — コメント本文
  • $user_ip (string) — ユーザーのIPアドレス
  • $user_agent (string) — ユーザーエージェント

例1: 基本的な使用法

コメントが禁止リストに含まれているかチェックします。

if (wp_check_comment_disallowed_list('author', 'test@example.com', 'http://example.com', 'comment', '127.0.0.1', 'Mozilla')) { echo '禁止リストに含まれています'; }

例2: 変数を使用したチェック

変数を使ってコメントをチェックします。

$check = wp_check_comment_disallowed_list($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent);

例3: スパム防止

コメント投稿時にスパムチェックを行います。

add_filter('preprocess_comment', function($commentdata) { if (wp_check_comment_disallowed_list($commentdata['comment_author'], $commentdata['comment_author_email'], $commentdata['comment_author_url'], $commentdata['comment_content'], $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'])) { wp_die('コメントが禁止されています'); } return $commentdata; });

例4: 特定のフィールドのみチェック

メールアドレスのみをチェックします。

wp_check_comment_disallowed_list('', 'spam@example.com', '', '', '', '');

例5: カスタムエラーメッセージ

禁止リストに含まれる場合にカスタムメッセージを表示します。

if (wp_check_comment_disallowed_list($author, $email, $url, $comment, $ip, $agent)) { $error = new WP_Error(); $error->add('disallowed', 'このコメントは許可されていません'); wp_die($error); }

例6: 管理画面での使用

管理画面でコメントをチェックします。

add_action('admin_init', function() { $result = wp_check_comment_disallowed_list(get_comment_author(), get_comment_author_email(), get_comment_author_url(), get_comment_text(), get_comment_author_IP(), get_comment_agent()); if ($result) { add_settings_error('disallowed_comment', 'disallowed_comment_error', '禁止されたコメントが見つかりました', 'error'); } });

例7: ログ記録

禁止リストにヒットしたコメントをログに記録します。

if (wp_check_comment_disallowed_list($author, $email, $url, $comment, $ip, $agent)) { error_log('禁止コメント: ' . $comment); }

注意点

  • この関数はスパム対策として使用できますが、完全な保護を保証するものではありません
  • 禁止リストはWordPressの設定で管理されています
  • 大量のコメントをチェックする場合、パフォーマンスに影響する可能性があります

関連機能: