Publish: 2016-05-18 | Modify: 2017-06-21
To enable IP location query in the WordPress backend, we need to import the Jquery library and load the necessary JavaScript functions. Open the admin-footer.php
file located at /wp-admin/admin-footer.php
and add the following code at the end, just before the closing </body>
tag:
<script src="https://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
<script>
function ipquery(ip, num) {
var ip = ip;
$.get('./ip-query.php', {ip: ip}, function(data, status) {
var address = new Function('return' + data)();
var guojia = address['data']['country'];
var sheng = address['data']['region'];
var shi = address['data']['city'];
var isp = address['data']['isp'];
var ip_add = guojia + ' ' + sheng + ' ' + shi + ' ' + isp;
num = '#' + num;
$(num).html(ip_add);
});
}
</script>
Save the following code as ip-query.php
and upload it to the /wp-admin/
directory. This file will be used for IP location queries.
<?php
$ip = $_GET['ip']; // Get IP
// Query IP location using Taobao IP interface
$get_ipquery = "http://ip.taobao.com//service/getIpInfo.php?ip=$ip";
// CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_ipquery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
?>
Open the class-wp-comments-list-table.php
file located at wp-admin/includes/class-wp-comments-list-table.php
and use a text editor to search for the keyword get_comment_author_IP
. After finding the corresponding line of code:
printf( '<a href="%s">%s</a>', esc_url( $author_ip_url ), $author_ip );
Add the following code right after it:
$num = 'abc'.rand(1000,9999);
echo " | <a href='javascript:;' onclick='return ipquery(\"$author_ip\",\"$num\");'>Query</a>";
echo "<div id='$num'></div>";
Make sure to save and overwrite the original file. The encoding format should be UTF-8 to avoid any character encoding issues. Don't forget to backup the original file before making any modifications.
After completing the above steps, open the comments section in the WordPress backend. You will notice a new "Query" button. Clicking on it will display the visitor's IP location. It's that simple! If you have any questions, feel free to leave a comment.
Please note that the WordPress backend files in the wp-admin
directory are numerous, so be careful not to modify the wrong file. Once again, it is essential to back up the original files before making any changes. Keep in mind that this method may be overwritten during a WordPress backend update. Consider creating a plugin to ensure the functionality remains intact.
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.