How Do I Block Countries on My WordPress Site?

Blocking Countries on Your WordPress Site
Blocking countries on your WordPress site can help to keep unwanted traffic from accessing your site. To block a country from accessing your site, add the following line of code to your WordPress theme’s functions.php file:

add_filter(‘access_deny_countries’, ‘my_access_deny_countries’);

my_access_deny_countries()

function my_access_deny_countries() {

$access_deny = array();

$access_deny[‘JP’] = array(

‘title’ => ‘日本’,

‘description’ => ‘日本のサイトはブロックされています。’,

‘access’ => ‘DENY’,

‘priority’ => ‘0’,

‘exclude_pages’ => array(

‘wp-admin/admin-ajax.php’

),

);

$access_deny[‘MY’] = array(

‘title’ => ‘マネージャーズ’,

‘description’ => ‘マネージャーズのサイトはブロックされています。’,

return $access_deny;

}

This function will create an access_deny_countries array and add the two countries to it. The array will have the following keys: title, description, access, priority, and exclude_pages. The title and description keys will be the same for both countries, while the access and priority keys will be different.

The exclude_pages key will be empty for Japan since it doesn’t have any excluded pages. The access_deny_countries array will be returned to the caller.