Toolsnip

Wordpress: Block Specific IP Addresses

Enhance your WordPress site's security by blocking specific IP addresses with this easy-to-use snippet, preventing unwanted access and spam.

Blocking specific IP addresses can be crucial for preventing spam, malicious access, or to enforce geographical content restrictions. This snippet allows you to block specific IPs from accessing your WordPress site.

The init hook is used to check the IP address of the visitor against a predefined list of blocked IPs. If the visitor's IP is in the list, they are redirected to a 403 Forbidden page.

This method is effective for quickly mitigating potential threats or unwanted traffic without requiring complex firewall rules or additional plugins.

The snippet provides a straightforward way to update the list of blocked IPs and includes recommendations for managing changes over time, such as adding or removing IPs based on site analytics.

Additional considerations are offered on how to handle false positives and ensuring that legitimate users are not accidentally blocked, along with tips for communicating these policies to users.

Snippet Code

Version Compatibility

This snippet is suitable for WordPress 2.0 and later, which supports robust IP handling and server interactions.

Required PHP Version

PHP 5.2 or higher

Wordpress Core Functions Used

Use Cases

  • security enhancements
  • access control
  • spam prevention