Country Change based on user ip

[ad_1]

Hi @ali199818

I hope you’re well today!

This code is only partially correct. it does get the IP and gets the country out of it but it won’t correctly select it.

The main issue here is this line:

$html = str_replace('field=""',"field=$country", $html);

which doesn’t really set anything “meaningful”.

Instead, you’d need to set default option value, remove “selected” flag for option already selected by default and then set it for matching country.

So in the code please replace above line with this three lines:

$html = str_replace('data-default-value=""','data-default-value="'.$country.'"', $html);
$html = str_replace( 'selected>', '', $html);
$html = str_replace( 'value="'.$country.'"', 'value="'.$country.'" selected', $html);

This should do the trick and works fine in my tests.

Best regards,
Adam

thank you so muck it wroks, but one more question how can i do it for city?

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer