Skip to content

Instantly share code, notes, and snippets.

@TheBinitGhimire
Last active August 4, 2020 22:13
Show Gist options
  • Save TheBinitGhimire/8dac2b929e44343766414f601513f0bb to your computer and use it in GitHub Desktop.
Save TheBinitGhimire/8dac2b929e44343766414f601513f0bb to your computer and use it in GitHub Desktop.
Get Comment Author Country from IP Address in WordPress
<?php
/* Place the following code in the functions.php file of your theme! */
/*
*
* Get Comment Author Country from IP Address | START
* Author: https://github.com/TheBinitGhimire/
* Source: https://gist.github.com/TheBinitGhimire/8dac2b929e44343766414f601513f0bb
*
* */
function get_comment_author_country($userIP){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ipapi.co/".$userIP."/country_name");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$userData = curl_exec($ch);
curl_close($ch);
return esc_html($userData);
}
add_action('wp_head', 'get_comment_author_country', 10, 1);
/*
*
* Get Comment Author Country from IP Address | END
*
* */
?>
<?php
/* Place the following line of code in the file in your theme where you are willing to display the comment author's country! */
echo esc_html(get_comment_author_country($comment->comment_author_IP));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment