# Send Comments With Links in the Name Field to Spam

> Marks comments as spam when the name field contains a web address, a classic trick of spam bots.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, spam, filter
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/spam-links-in-commenter-name/

## Code

```php
add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) {
    if ( is_wp_error( $approved ) || ! empty( $commentdata['user_id'] ) ) {
        return $approved;
    }

    $name = isset( $commentdata['comment_author'] ) ? (string) $commentdata['comment_author'] : '';

    return preg_match( '#https?://|www\.|\.(com|net|ru|xyz|top)\b#i', $name ) ? 'spam' : $approved;
}, 20, 2 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send Comments With Links in the Name Field to Spam" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
