Using WordPress ‘comment_edit_pre’ PHP filter

The comment_edit_pre WordPress PHP filter allows you to modify the comment content before it gets edited.

Usage

add_filter('comment_edit_pre', 'your_custom_function');
function your_custom_function($comment_content) {
    // your custom code here
    return $comment_content;
}

Parameters

  • $comment_content (string) – The comment content to be filtered.

More information

See WordPress Developer Resources: comment_edit_pre

Examples

Replace specific words in the comment

This example replaces the word “bad” with “good” in the comment content before editing.

add_filter('comment_edit_pre', 'replace_words_in_comment');
function replace_words_in_comment($comment_content) {
    $comment_content = str_replace('bad', 'good', $comment_content);
    return $comment_content;
}

Add a prefix to the comment

This example adds a prefix “Edited: ” to the comment content before editing.

add_filter('comment_edit_pre', 'add_prefix_to_comment');
function add_prefix_to_comment($comment_content) {
    $comment_content = 'Edited: ' . $comment_content;
    return $comment_content;
}

Remove all HTML tags from the comment

This example removes all HTML tags from the comment content before editing.

add_filter('comment_edit_pre', 'remove_html_from_comment');
function remove_html_from_comment($comment_content) {
    $comment_content = strip_tags($comment_content);
    return $comment_content;
}

Convert all text to uppercase

This example converts all the text in the comment content to uppercase before editing.

add_filter('comment_edit_pre', 'convert_comment_to_uppercase');
function convert_comment_to_uppercase($comment_content) {
    $comment_content = strtoupper($comment_content);
    return $comment_content;
}