如何限制WordPress評論留言的內容長度

如何限制WordPress評論留言的內容長度

您是否厭倦了處理 WordPress 網站上過長或無益的評論?限制評論長度可以大大提高討論質量,減少垃圾評論。

在本指南中,我將向您介紹為 WordPress 評論設定最小和最大字元限制的簡單步驟。

為什麼要限制評論長度?

活躍的評論區是促進參與和建立部落格社羣的好方法。然而,並非所有評論都有價值。以下是限制評論長度的幾個原因:

  • 單詞評論通常沒有幫助,而且往往是試圖獲取反向連結的垃圾評論。
  • 超長評論(超過 5000 個字元)通常是咆哮、抱怨或與文章無關。
  • 短評論(60 個字元以下)很少能提供有意義的見解或反饋。

通過設定評論長度限制,您可以阻止垃圾郵件、咆哮和低質量評論,最終提高網站的整體討論質量。

在WordPress中設定評論長度限制

遺憾的是,WordPress 沒有提供限制評論長度的內建方法。不過,您可以通過在網站上新增程式碼片段來輕鬆實現這一功能。為確保安全並避免破壞網站,我們建議使用程式碼片段外掛。

  1. 安裝並啟用 Code Snippets 外掛。您可以在我們的新手指南中找到關於如何安裝 WordPress 外掛的詳細說明。
  2. 啟用後,進入 WordPress 管理區的 Code Snippets > + Add Snippet
  3. 點選 Add Your Custom Code (New Snippet) 下的 Use Snippet 按鈕。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress
add_filter( 'preprocess_comment', 'smartwp_limit_comment_length' );
function smartwp_limit_comment_length( $comment ) {
// Limit the comments to 6000 characters
if ( strlen( $comment['comment_content'] ) > 6000 ) {
wp_die('Comment is too long. Comments must be under 6000 characters.');
}
// Require 50 characters to leave a comment
if ( strlen( $comment['comment_content'] ) < 50 ) {
wp_die('Comment is too short. Comments must be at least 50 characters.');
}
return $comment;
}
<?php // Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress add_filter( 'preprocess_comment', 'smartwp_limit_comment_length' ); function smartwp_limit_comment_length( $comment ) { // Limit the comments to 6000 characters if ( strlen( $comment['comment_content'] ) > 6000 ) { wp_die('Comment is too long. Comments must be under 6000 characters.'); } // Require 50 characters to leave a comment if ( strlen( $comment['comment_content'] ) < 50 ) { wp_die('Comment is too short. Comments must be at least 50 characters.'); } return $comment; }
<?php
// Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress
add_filter( 'preprocess_comment', 'smartwp_limit_comment_length' );
function smartwp_limit_comment_length( $comment ) {
  
  // Limit the comments to 6000 characters
  if ( strlen( $comment['comment_content'] ) > 6000 ) {
    wp_die('Comment is too long. Comments must be under 6000 characters.');
  }
  
  // Require 50 characters to leave a comment
  if ( strlen( $comment['comment_content'] ) < 50 ) {
    wp_die('Comment is too short. Comments must be at least 50 characters.');
  }
  return $comment;
}

修改數字 6000 和 50,分別設定所需的最大和最小字元限制。

就是這樣!現在,您的評論長度限制已經生效。當使用者嘗試提交過短或過長的評論時,他們將看到您設定的自定義錯誤資訊。

條件邏輯(可選)

如果只想限制特定頁面或文章的評論長度,可以使用程式碼片段中的條件邏輯功能。啟用 Conditional Logic 切換,並設定程式碼片段的執行條件(如特定頁面 URL)。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress for a specific post
add_filter('preprocess_comment', 'smartwp_limit_comment_length');
function smartwp_limit_comment_length($comment) {
// Specify the post ID where you want this function to run
$specific_post_id = 123; // Replace 123 with your desired post ID
// Check if the current post is the specific post we want to target
if (get_the_ID() == $specific_post_id) {
// Limit the comments to 6000 characters
if (strlen($comment['comment_content']) > 6000) {
wp_die('Comment is too long. Comments must be under 6000 characters.');
}
// Require 50 characters to leave a comment
if (strlen($comment['comment_content']) < 50) {
wp_die('Comment is too short. Comments must be at least 50 characters.');
}
}
return $comment;
}
<?php // Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress for a specific post add_filter('preprocess_comment', 'smartwp_limit_comment_length'); function smartwp_limit_comment_length($comment) { // Specify the post ID where you want this function to run $specific_post_id = 123; // Replace 123 with your desired post ID // Check if the current post is the specific post we want to target if (get_the_ID() == $specific_post_id) { // Limit the comments to 6000 characters if (strlen($comment['comment_content']) > 6000) { wp_die('Comment is too long. Comments must be under 6000 characters.'); } // Require 50 characters to leave a comment if (strlen($comment['comment_content']) < 50) { wp_die('Comment is too short. Comments must be at least 50 characters.'); } } return $comment; }
<?php
// Limit the comment length to 6000 characters and a minimum of 50 characters in WordPress for a specific post
add_filter('preprocess_comment', 'smartwp_limit_comment_length');

function smartwp_limit_comment_length($comment) {
    // Specify the post ID where you want this function to run
    $specific_post_id = 123; // Replace 123 with your desired post ID

    // Check if the current post is the specific post we want to target
    if (get_the_ID() == $specific_post_id) {
        // Limit the comments to 6000 characters
        if (strlen($comment['comment_content']) > 6000) {
            wp_die('Comment is too long. Comments must be under 6000 characters.');
        }

        // Require 50 characters to leave a comment
        if (strlen($comment['comment_content']) < 50) {
            wp_die('Comment is too short. Comments must be at least 50 characters.');
        }
    }

    return $comment;
}

小結

限制評論長度是提高 WordPress 網站討論質量的一種簡單而有效的方法。通過設定合理的最小和最大字元限制,您可以攔截垃圾郵件和低質量評論,最終為讀者建立一個更有吸引力和價值的評論區。

評論留言

脣槍舌劍 (1)