Function Signature:
wp_filter_comment
Function Description:
Filters and sanitizes comment data.
Function Examples:
⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Filtering a comment to remove specific words before saving it
add_filter( 'wp_filter_comment', 'filter_comment_content' );
function filter_comment_content( $comment_data ) {
$comment_content = $comment_data['comment_content'];
$filtered_content = str_replace( 'badword', '', $comment_content );
$comment_data['comment_content'] = $filtered_content;
return $comment_data;
}
// Example 2: Restricting comments from being saved if they contain links
add_filter( 'wp_filter_comment', 'restrict_comments_with_links' );
function restrict_comments_with_links( $comment_data ) {
$comment_content = $comment_data['comment_content'];
if ( preg_match( '/http(s)?:\/\//', $comment_content ) ) {
wp_die( 'Comments with links are not allowed.' );
}
return $comment_data;
}
// Example 3: Customizing the comment author name before saving it
add_filter( 'wp_filter_comment', 'customize_comment_author_name' );
function customize_comment_author_name( $comment_data ) {
$comment_author = $comment_data['comment_author'];
$custom_author = 'User_' . $comment_author;
$comment_data['comment_author'] = $custom_author;
return $comment_data;
}