Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressでコメントの長さを制限する方法(簡単チュートリアル)

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressでコメントする長さを制限したいですか?

WordPressのコメントは、ブログ投稿コンテンツに関するディスカッションを促進します。しかし、非常に短いコメントや長すぎるコメントはあまり役に立たないと感じるかもしれません。

この投稿では、WordPressで簡単にコメントの長さを制限する方法を紹介します。

Limit Comment Length in WordPress

なぜWordPressでコメントする長さを制限するのか?

有効化したコメントエリアは、WordPressブログのコミュニティを構築するのに最適な方法です。訪問者は、フィードバックや質問をしたり、トピックに関する独自の視点を提供することができます。

しかし、すべてのコメントが参考になるわけではない。

私たちは10年以上WordPressのコメントをモデレートしてきました。私たちの経験では、最も役に立つコメントは60文字以上で5000文字以下であることがわかりました。

一言コメントは通常あまり役に立ちません。ほとんどの場合、投稿者はあなたのサイトからのバックリンクが欲しいだけのスパムコメントです。

一方、5,000文字以上の長いコメントは、暴言や苦情であることが多い。投稿と関係ないこともある。

WordPressでコメントの長さ制限を設定することで、コメント全体の質を向上させ、スパムコメントを防ぐことができます。しかし、WordPressにはこれを行うビルトインの方法はありません。

ということで、WordPressで最小と最大の制限を設定してコメントの長さをコントロールする方法を見てみましょう。

WordPressでコメントの長さを制限する方法

WordPressでコメントする長さを制限するには、functions.phpファイルにコードを追加します。ただし、コードを入力する際のわずかなエラーでサイトが壊れてアクセスできなくなる可能性があることに留意してください。

そのため、WordPressサイトにコードスニペットを挿入するには、常にWPCodeを使用することをお勧めします。これは、カスタマイザーコードを安全かつ簡単に追加できる、市場で最高のWordPressコードスニペットプラグインです。

まず、WPCodeプラグインをインストールし、有効化する必要があります。詳しい手順は、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

:WPCodeには、このチュートリアルで使用できる無料版もあります。しかし、有料プランにアップグレードすると、より大きなコードスニペットライブラリ、条件ロジックなど、より多くの機能を利用できるようになります。

有効化した後、WordPress管理サイドバーからCode Snippets ” + Add Snippetページにアクセスしてください。

ここで、「カスタムコードを追加(新規スニペット)」設定の下にある「スニペットを使用」ボタンをクリックします。

Add new snippet

カスタムスニペットの作成」ページが表示されますので、まずはコードスニペットのタイトルを追加してください。この名前はサイトのフロントエンドには表示されません。

次に、画面右隅のドロップダウンメニューから、コードタイプとして「PHPスニペット」オプションを選択します。

Choose the PHP Snippet option for comment length limit

それができたら、以下のコード・スニペットをコピー&ペーストして「コード・プレビュー」ボックスに入れるだけです:

add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
 
function wpb_preprocess_comment($comment) {
    if ( strlen( $comment['comment_content'] ) > 5000 ) {
        wp_die('Comment is too long. Please keep your comment under 5000 characters.');
    }
if ( strlen( $comment['comment_content'] ) < 60 ) {
        wp_die('Comment is too short. Please use at least 60 characters.');
    }
    return $comment;
}

このコードスニペットは、preprocess_commentにフィルターフックを追加することで動作します。このフィルターは WordPress がコメントをデータベースに保存したり、送信されたコメントに対して前処理を行ったりする前に実行されます。

コメントする長さをチェックし、短すぎたり長すぎたりするとエラーメッセージを表示します。初期設定では、このスニペットではコメント制限を最小60文字、最大5,000文字に設定しています。

しかし、独自のコメント制限を設定するには、コード内の5,000という数字を、あなたの最大制限数に置き換えるだけです。

同様に、コード内の60という数字を置き換えることで、WordPressサイトで異なる最低コメント制限を設定することができます。

また、ユーザーがコメント制限を超えたり、下回ったりしたときにサイトに表示されるメッセージを変更することもできます。コードのwp_die行の後に表示したい文章をタイプするだけです。

Edit comment limit snippet

その後、「インサーター」セクションまでスクロールダウンし、「自動挿入」モードを選択する。

カスタムコードは有効化した時点で自動的にあなたのサイトで実行されます。

Choose an insertion method

特定のサイトページのみコメントする長さを制限したい場合は、そのようにすることもできます。

条件付きロジック」セクションまでスクロールダウンし、「ロジックを有効化」スイッチを切り替えるだけです。

その後、「Conditions(条件)」のドロップダウンメニューから「Show(表示)」オプションを選択し、「+ Add new group(新規グループを追加)」ボタンをクリックする。

Enable the Conditional Logic toggle

新しいタブが開きますので、左のドロップダウンメニューから「ページURL」オプションを設定してください。

次に、コメントする長さを制限したいページのURLを右側のフィールドに入力します。

これで、コード・スニペットは今入力したURLのページでのみ有効化したことになる。

Type the conditional logic

ページを一番上までスクロールし、「Inactive」スイッチを「Active」に切り替える。

最後に「Save Snippet」ボタンをクリックして、設定を保存します。

Save the comment limit snippet

これで、ユーザーが最低コメント長に満たないコメントを入力すると、画面にこのメッセージが表示されます。

ユーザーは、最低でもあなたが選んだ長さにならないとコメントを投稿することができません。

An Error Message Is Displayed if a Comment is Too Short or Too Long

同様に、ユーザーが上限を超えるコメントを入力すると、このメッセージが画面に表示されます。

そうすることで、サイト上で暴言やスパムコメントを減らすことができます。

Message preview for a long comment

ボーナス:WordPressサイトのコメントエンゲージメントを向上させる

コメントの長さをコントロールすることは、WordPressのコメント欄のエンゲージメントを高める一つの方法です。ユーザーのコメントに関連キーワードが含まれていたり、コンテンツに文脈が追加されていたりする場合、サイト訪問者を長く滞在させることができ、サイトのSEOにも役立ちます。

また、Thrive Commentsを使えば、サイトのコメント欄を簡単に改善することができます。

The Thrive Comments WordPress plugin

専用のモデレーションボードが付属し、コメントを遅延ロードすることができ、ユーザーがソーシャルメディアのプロフィールと一緒にコメントを残すことができるなど、最高のWordPressコメントプラグインです。

さらに、このツールは、スパムを減らし、あなたのサイト上で興味深いコメントを奨励するために、アップボーテ/ダウンボーテ機能を追加することが有効化されます。

Thrive Commentsでは、励ましのコメントを上部に表示し、不快なコメントや無関係なコメントをディスカッションセクションの下部に表示することもできます。

Feature comment from dropdown menu

これにより、ディスカッションに最も価値を与えているユーザーに報酬を与える一方、他のユーザーが有益でないコメントを残すのを丁重に阻止することができます。

詳細な手順については、WordPressでコメントを目立たせたり埋めたりする方法のチュートリアルをご覧ください。

WordPressでコメントの長さを制限する方法について、このチュートリアルがお役に立てれば幸いです。ブログのトラフィックを増やす方法や、サイトを成長させるのに最適なWordPressプラグインのリストもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

20件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Harsh Lahre says

    Thank you for this code, I was tired deleting comments, each one is more than 20 thousand characters. This helps me a lot.

  3. connie mergel says

    The control comment length plugin is no longer available> Do you have any suggestions about another plugin that will do the same thing.

    Thanks

    • WPBeginner Support says

      While we don’t have a recommendation at the moment, we will be sure to update this article when we find a plugin we would recommend.

      管理者

    • WPBeginner Support says

      Thank you for letting us know, we’ll be sure to keep an eye out for an alternative plugin :)

      管理者

  4. ghost says

    hi,
    first thanks for your code
    and how can i stay in my page and open a popup message showing the message error ?

    thanks

  5. Charlie Lynburg says

    I am thrilled to have found this! It’s life-changing in a site like mine!

    Can you give me a code to add that EXEMPTS this limit for myself (admins)? Thanks

  6. Bharat Sharma says

    Hi,

    The plugin is no longer in WordPress directory, but the code works very well. However, I want to know is there a way we can “gray out” or “disable” the submit button if the comment length conditions are not met. Right now, what this code do is it allow the user to submit comment and they get an error message if their comment length is not as per code criteria. By greying out the button we will be able to stop users from getting irritated and their will be no need to hit the back button.

  7. stanley says

    hi where can i download the wordpress application?
    can you give the link?or send it thru my gmail? thanks

    • paunkumar says

      add_filter( ‘preprocess_comment’, ‘wpb_preprocess_comment’ );

      function wpb_preprocess_comment($comment) {
      if ( strlen( $comment[‘comment_content’] ) > 5000 ) {
      wp_die(‘Comment is too long. Please keep your comment under 5000 characters.’);
      }
      if ( strlen( $comment[‘comment_content’] ) < 60 ) {
      wp_die('Comment is too short. Please use at least 60 characters.');
      }
      return $comment;
      }

  8. Tom says

    Is there any way to limit comments, and if the limit is exceeded a new page will be created and the rest of the comment is on a new page?

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。