Random WordPress Function

Learn about a new WordPress function every day!


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: Preventing comments with specific URLs from being published
add_filter( 'wp_filter_comment', 'filter_comment_url' );
function filter_comment_url( $commentdata ) {
    if ( strpos( $commentdata['comment_content'], 'example.com' ) !== false ) {
        $commentdata['comment_approved'] = 'spam';
    }
    return $commentdata;
}
// Example 3: Customizing the comment author's name before saving
add_filter( 'wp_filter_comment', 'filter_comment_author' );
function filter_comment_author( $commentdata ) {
    $commentdata['comment_author'] = 'Custom Name';
    return $commentdata;
}