Created
December 31, 2023 18:53
-
-
Save saroarhossain57/b372876b538e8ff767ecd8b79b1933f4 to your computer and use it in GitHub Desktop.
Switch theme based on visitor location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$themes_settings = [ | |
'default_theme' => 'newsup', | |
'country_based_themes' => [ | |
"UK" => "blogus", | |
"BD" => "electro", | |
] | |
]; | |
function get_ip_location(){ | |
$client_ip = @$_SERVER['HTTP_CLIENT_IP']; | |
$forward_ip = @$_SERVER['HTTP_X_FORWARDED_FOR']; | |
$remote_ip = @$_SERVER['REMOTE_ADDR']; | |
$result = array('country_code'=>'', 'country_name'=>'', 'region'=>'', 'city'=>''); | |
if(filter_var($client_ip, FILTER_VALIDATE_IP)){ | |
$ip_address = $client_ip; | |
} elseif(filter_var($forward_ip, FILTER_VALIDATE_IP)){ | |
$ip_address = $forward_ip; | |
} else{ | |
$ip_address = $remote_ip; | |
} | |
$loc_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip_address)); | |
if($loc_data && $loc_data->geoplugin_countryName != null){ | |
$result['country_code'] = $loc_data->geoplugin_countryCode; | |
$result['country_name'] = $loc_data->geoplugin_countryName; | |
$result['region'] = $loc_data->geoplugin_region; | |
$result['city'] = $loc_data->geoplugin_city; | |
} else{ | |
$result['country_code'] = 'US'; | |
$result['country_name'] = 'United State'; | |
} | |
return $result; | |
} | |
$visitor_data = get_ip_location(); | |
$selected_theme = isset($themes_settings['default_theme']) ? $themes_settings['default_theme'] : null; | |
if(isset($themes_settings['country_based_themes']) && is_array($themes_settings['country_based_themes'])){ | |
foreach($themes_settings['country_based_themes'] as $country_code => $theme_slug){ | |
if($visitor_data['country_code'] == $country_code){ | |
$selected_theme = $theme_slug; | |
} | |
} | |
} | |
// Switch the theme | |
if($selected_theme){ | |
switch_theme($selected_theme); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment