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 profanity
add_filter( 'wp_filter_comment', 'filter_comment_content' );
function filter_comment_content( $commentdata ) {
$commentdata['comment_content'] = preg_replace('/badword/', '***', $commentdata['comment_content']);
return $commentdata;
}
// Example 2: Limiting the length of comments
add_filter( 'wp_filter_comment', 'limit_comment_length' );
function limit_comment_length( $commentdata ) {
if ( strlen( $commentdata['comment_content'] ) > 200 ) {
$commentdata['comment_content'] = substr( $commentdata['comment_content'], 0, 200 ) . '...';
}
return $commentdata;
}
// Example 3: Preventing comments from users with specific email domains
add_filter( 'wp_filter_comment', 'block_email_domain' );
function block_email_domain( $commentdata ) {
$blocked_domains = array( 'example.com', 'test.com' );
$email_domain = explode( '@', $commentdata['comment_author_email'] )[1];
if ( in_array( $email_domain, $blocked_domains ) ) {
$commentdata['comment_approved'] = 'spam';
}
return $commentdata;
}