Using WordPress ‘preprocess_comment’ PHP filter

apply_filters(‘preprocess_comment’, array $commentdata) is a WordPress PHP filter that modifies a comment’s data before it gets sanitized and inserted into the database.

Usage

To use this filter, create a custom function that modifies the $commentdata array and hook it to the filter with the add_filter function.

function custom_preprocess_comment($commentdata) {
    // Modify $commentdata here
    return $commentdata;
}
add_filter('preprocess_comment', 'custom_preprocess_comment');

Parameters

  • $commentdata (array): An array containing the comment data.

The $commentdata array contains the following keys:

  • ‘comment_post_ID’: The post to which the comment will apply.
  • ‘comment_author’: (may be empty)
  • ‘comment_author_email’: (may be empty)
  • ‘comment_author_url’: (may be empty)
  • ‘comment_content’: The text of the proposed comment.
  • ‘comment_type’: ‘pingback’, ‘trackback’, or empty for regular comments.
  • ‘user_ID’: (empty if not logged in)

Examples

Adding a prefix to comment author names

function prefix_comment_author($commentdata) {
    $commentdata['comment_author'] = 'Guest: ' . $commentdata['comment_author'];
    return $commentdata;
}
add_filter('preprocess_comment', 'prefix_comment_author');

This function adds the prefix “Guest: ” to the comment author’s name before saving the comment.

Moderate comments containing certain keywords

function moderate_keywords_in_comments($commentdata) {
    $keywords = array('badword1', 'badword2', 'badword3');

    foreach ($keywords as $keyword) {
        if (strpos($commentdata['comment_content'], $keyword) !== false) {
            $commentdata['comment_approved'] = 0;
            break;
        }
    }
    return $commentdata;
}
add_filter('preprocess_comment', 'moderate_keywords_in_comments');

This function checks if the comment contains any of the specified keywords. If so, it sets the comment as unapproved.

function convert_urls_to_links($commentdata) {
    $commentdata['comment_content'] = preg_replace(
        '/(http|https|ftp|ftps)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/S*)?/',
        '<a href="$0" target="_blank">$0</a>',
        $commentdata['comment_content']
    );
    return $commentdata;
}
add_filter('preprocess_comment', 'convert_urls_to_links');

This function converts URLs in the comment content to clickable hyperlinks before saving the comment.

Limiting comment length

function limit_comment_length($commentdata) {
    $maxLength = 500;

    if (strlen($commentdata['comment_content']) > $maxLength) {
        $commentdata['comment_content'] = substr($commentdata['comment_content'], 0, $maxLength);
    }
    return $commentdata;
}
add_filter('preprocess_comment', 'limit_comment_length');

This function limits the comment length to a specified maximum length before saving the comment.

Removing HTML tags from comments

function remove_html_from_comments($commentdata) {
    $commentdata['comment_content'] = strip_tags($commentdata['comment_content']);
    return $commentdata;
}
add_filter('preprocess_comment', 'remove_html_from_comments');

This function removes all HTML tags from the comment content before saving the comment.

Replacing specific words with alternatives

function replace_words_in_comments($commentdata) {
    $word_replacements = array(
        'oldword1' => 'newword1',
        'oldword2' => 'newword2',
        'oldword3' => 'newword3'
    );

    $commentdata['comment_content'] = str_replace(array_keys($word_replacements), array_values($word_replacements), $commentdata['comment_content']);
    return $commentdata;
}
add_filter('preprocess_comment', 'replace_words_in_comments');

This function replaces specific words in the comment content with their alternatives before saving the comment.

Adding a custom message to comments by logged-in users

function add_message_for_logged_in_users($commentdata) {
    if (!empty($commentdata['user_ID'])) {
        $commentdata['comment_content'] .= "nnMessage: This comment was submitted by a registered user.";
    }
    return $commentdata;
}
add_filter('preprocess_comment', 'add_message_for_logged_in_users');

This function appends a custom message to the comments submitted by logged-in users before saving the comment.

Converting all-caps text to lowercase in comments

function convert_all_caps_to_lowercase($commentdata) {
    if (strtoupper($commentdata['comment_content']) == $commentdata['comment_content']) {
        $commentdata['comment_content'] = strtolower($commentdata['comment_content']);
    }
    return $commentdata;
}
add_filter('preprocess_comment', 'convert_all_caps_to_lowercase');

This function converts comments written in all-caps to lowercase before saving the comment.

Adding a timestamp to comment content

function add_timestamp_to_comments($commentdata) {
    $timestamp = date('Y-m-d H:i:s');
    $commentdata['comment_content'] .= "nnSubmitted on: $timestamp";
    return $commentdata;
}
add_filter('preprocess_comment', 'add_timestamp_to_comments');

This function appends the current timestamp to the comment content before saving the comment.

Restricting comments to specific languages

function restrict_comments_to_languages($commentdata) {
    $allowed_languages = array('en', 'es', 'fr');
    $detected_language = detect_comment_language($commentdata['comment_content']); // Assuming a custom function to detect the comment's language

    if (!in_array($detected_language, $allowed_languages)) {
        $commentdata['comment_approved'] = 0;
    }
    return $commentdata;
}
add_filter('preprocess_comment', 'restrict_comments_to_languages');

This function checks if the comment’s language is in the allowed languages list. If not, it sets the comment as unapproved.