Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Close comments on posts older than 30 days

New comments are only accepted on posts published in the last 30 days. Older posts keep their comments, but the form closes. Most spam goes to old posts.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Category Comments
  • Tested with WordPress 7.1
PHP
<?php

// The same setting as Settings > Discussion, fixed in code.
add_filter( 'pre_option_close_comments_for_old_posts', static fn() => 1 );
add_filter( 'pre_option_close_comments_days_old', static fn() => 30 );

Good to know

Change 30 to the number of days you want. Pages and products follow the same rule.

In these packs

  • Comments: Fewer spam comments and more control: close old posts, set minimum and maximum lengths, a spam trap, no links.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.