Just imagine this scenario. Someone has activated Google AdSense for his/her website after getting approval for an AdSense account from Google. However, just three days after the ad was launched, his account became “banned” for the rest of his life due to ‘Invalid Click’.
He speculates that many clients were given Internet connection by sharing the same IP address by his Internet Provider.
Some of these users have clicked on the Google Ad on the person’s website. As a result, Google has banned the victim’s AdSense account.
As a result, Google will treat other people’s clicks of the same IP address as the person who created the AdSense account – in simple terms, Google will assume that the publisher himself clicked on his ad, even if he did not click his AD.
Well, this can be an example, but there are instances when you don’t want certain content to be shown to users with certain IP addresses. The following code snippet will help you handle the situation:
There are many versions of the IP tracking function with PHP, I actually found this function effective most of the time (though sometimes failing).
And, we’ve also created another function wherein we specify the IP constraints that we want to block in the form of arrays (one if one, multiple if multiple).
This function will match the visitor’s IP address in one or more of the IP addresses I am trying to block, if matched, return false, and return true if not found.
And the function can be used like this:
How to use it in WordPress?
For Themes: You can place both functions in the theme’s functions.php and use them throughout the theme.
For Plugins: You can place both functions in any file of the plugin and use the plugin even throughout the theme.