# Send Comments With Too Many Links to Spam

> Marks a comment as spam when it contains more links than you allow, catching most link-dropping spam before it reaches the moderation queue.

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

## Code

```php
// Most links a genuine comment may contain.
$wpide_max_comment_links = 2;

add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) use ( $wpide_max_comment_links ) {
    if ( is_wp_error( $approved ) || 'spam' === $approved || 'trash' === $approved || ! empty( $commentdata['user_id'] ) ) {
        return $approved;
    }

    $links = preg_match_all( '#(https?://|www\.)#i', (string) $commentdata['comment_content'] );

    return $links > $wpide_max_comment_links ? 'spam' : $approved;
}, 20, 2 );
```

## Notes

Logged-in users are never affected; check the Spam folder now and then for false positives.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send Comments With Too Many Links 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/
