WordPress + CSF: How to Block IPs After Failed Login Attempts


🛠️ Divi Builder drag & drop page builder for WP

A lot of servers rely on CSF and similar tools to reduce attacks on their resources. You can use it to block malicious IPs temporarily and permanently. We have already covered how you can combine it with Fail2Ban to temporary block bad bots. You can also use CSF to block IPs from which too many failed login attempts originate. To do this, you will have to create a function that catches the IP for each failed login attempt and passes them to a CSF function that adds them to your deny list. Here is what it looks like:

The csf_block_failed_logins function monitors failed login attempts and stores failed attempts for 1 hour using WordPress transients. Once that number exceeds 5, a CSF function is called to run the csf -d command.  How would you improve this code?

More WordPress reading: