skysurfer.media

Remove Unapproved


Now that my solution for auto deleting WordPress comments has been tested for a couple of weeks, this post is to share the code. This will probably be merged with other shared configurations on a dedicated page in the future.

The Problem: Automated bots don’t respect robots.txt and spammers find ways of bypassing spam lists. Today’s example appears to be trying to get me to go to a Russian site named Mr. Dick… dubious, right? And I don’t even speak Russian. If this was rare it wouldn’t be a big deal, but this happens daily from random places all over the world – and increases whenever I post. WordPress doesn’t offer a way to automatically manage comments, either, except through a paid plugin. And I’m sure I don’t need to know Mr. Dick.

The Solution: systemd timers – as follows, systemd timers require two files to function. This setup works on archlinux with mariadb hosting wordpress. Once a week, a command is run to scan the database for entries that have not been approved or otherwise marked. A similar command could be used to delete all comments, regardless.

/etc/systemd/system/delete-unapproved-comments.service

[Unit]
Description=Delete Unapproved WordPress Comments

[Service]
ExecStart=/bin/bash -c "mariadb --user=<DB_USER> --password=<DB_PASS> --host=localhost --database=wordpress --execute=\"DELETE FROM wp_comments WHERE comment_approved = '0';\""

/etc/systemd/system/delete-unapproved-comments.timer

[Unit]
Description=Timer for Delete Unapproved WordPress Comments

[Timer]
OnCalendar=Mon *-*-* 00:00:00
Persistent=true

[Install]
WantedBy=timers.target

I know, I could just turn off comments altogether since I don’t use them, but I’m happy to send the spam to /dev/null …for free. I’m posting this for reference. Adjustments can be made for different database operations and a lot more can be done with timers. This replaces cron.