Hide Content From One IP Address on WordPress Site

This gets the visitor’s IP address, and hides the content from the IP address that you specify. This is for use on WordPress sites. One way to use it is in your single.php template file (or content-single.php). Replace this:

the_content();

… with this:

function get_client_ip() {
     $ipaddress = '';
     if ($_SERVER['HTTP_CLIENT_IP'])
         $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
     else if($_SERVER['HTTP_X_FORWARDED_FOR'])
         $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
     else if($_SERVER['HTTP_X_FORWARDED'])
         $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
     else if($_SERVER['HTTP_FORWARDED_FOR'])
         $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
     else if($_SERVER['HTTP_FORWARDED'])
         $ipaddress = $_SERVER['HTTP_FORWARDED'];
     else if($_SERVER['REMOTE_ADDR'])
         $ipaddress = $_SERVER['REMOTE_ADDR'];
     else
         $ipaddress = 'UNKNOWN';

     return $ipaddress; 
}

$unwanted_visitor = '11.111.111.11'; // @todo change this regularly, since most IP addresses change.
if ( get_client_ip() == $unwanted_visitor ) {
	echo '<h3>YOU ARE NOT AUTHORIZED TO SEE THIS CONTENT</h3>';
} else {
	the_content();
}

Be sure to change the ‘11.111.111.11’ on line 21 above to the IP address that you are trying to block.

Two caveats

Most visitors have dynamic IP addresses which change often. So you may be blocking that unwanted visitor for this week, but that IP address may be assigned to a different visitor next week. Also, the same IP address can be assigned to many different users simultaneously. You may inadvertently block extra visitors with this.

Use this method only if you understand the implications.

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]