How to Blacklist an IP address and redirect them

1 minute read

If your website is having a lot of traffic from an IP address and you would like to redirect them to a landing page, here is the code to do it.

With this WordPress function which you can add to your functions.php file, you can add IP addresses into the array called $blocked_ips and change the URL to your landing page in the wp_redirect command.

function blacklisted_ip() {
    $blocked_ips = array(
    $user_ip = $_SERVER['REMOTE_ADDR'];

    if ( in_array( $user_ip, $blocked_ips  )) {

		global $wpdb;
		$d = date("d/m/y H:i:s");

		$wpdb->insert( $wpdb->prefix . "IP_blacklist" , array(
		'date' => $d ,
		'details' => $user_ip 
		), array( '%s', '%s' ) );
add_action('template_redirect', 'blacklisted_ip', 0);

If you have any questions please get in touch on social media, the buttons are below.
If you found this handy check out our post on Stop Fake Spam WooCommerce Orders too!