Delete Comments Left Unmoderated for 90 Days
Once a day, permanently deletes comments that have been waiting in the moderation queue for more than 90 days, along with the names, emails and IP addresses they contain.
Opens WPIDE on
Code
// Days a comment may wait for moderation.
$wpide_pending_days = 90;
add_action( 'init', function () {
if ( ! wp_next_scheduled( 'wpide_delete_stale_pending' ) ) {
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'wpide_delete_stale_pending' );
}
} );
add_action( 'wpide_delete_stale_pending', function () use ( $wpide_pending_days ) {
$ids = get_comments( array(
'status' => 'hold',
'number' => 200,
'fields' => 'ids',
'date_query' => array( array( 'column' => 'comment_date_gmt', 'before' => $wpide_pending_days . ' days ago' ) ),
) );
foreach ( $ids as $id ) {
wp_delete_comment( (int) $id, true );
}
} );
Notes
Approve or reply to comments you want to keep before they reach the age limit.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.